Home > Web Front-end > JS Tutorial > Different Ways to find maximum number in javascript

Different Ways to find maximum number in javascript

Barbara Streisand
Release: 2024-12-21 10:21:11
Original
265 people have browsed it

Different Ways to find maximum number in javascript

There are several ways to find the maximum number in javascript. they are as follows:

  1. Using Max.math() with spread operator :

let arr = [89, 3, 2, 66, 3, 64, 100, 35]
let max = Math.max(...arr)
console.log(max);

  1. Using reduce() reduce method can be used to accumulate the max value by comparing each element. let arr = [89, 3, 2, 66, 3, 64, 100, 35] let max = arr.reduce((acc, curr)=>(curr>acc?: curr: acc), -Infinity) console.log(max) @here - Infinity is used to ensure that the first comparision always return the first element
  2. using a For Loop: a ``

The above is the detailed content of Different Ways to find maximum number in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template