Home >Web Front-end >JS Tutorial >What is the maximum value statement in javascript?

What is the maximum value statement in javascript?

藏色散人
藏色散人Original
2021-06-18 11:37:072551browse

The maximum value statement in javascript is "Math.max(value1[,value2, ...])", where the "Math.max()" function is to return the maximum value in a set of numbers .

What is the maximum value statement in javascript?

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is the maximum value statement in javascript?

Math.max() function returns the maximum value in a set of numbers.

Syntax

Math.max(value1[,value2, ...])

Parameters

value1, value2, ...

A set of numerical values

Return value

Returns the maximum value in a given set of numbers . If at least one of the given arguments cannot be converted to a number, NaN is returned.

Description

Since max is a static method of Math, it should be used like this: Math.max(), not a method of the created Math instance (Math is not a constructor).

If there are no parameters, the result is - Infinity.

If any parameter cannot be converted to a numeric value, the result is NaN.

Example

Using Math.max()

Math.max(10, 20);   //  20
Math.max(-10, -20); // -10
Math.max(-10, 20);  //  20

The following method uses the apply method to find the largest element in a numeric array. getMaxOfArray([1,2,3]) is equivalent to Math.max(1, 2, 3), but you can use getMaxOfArray () to operate on arrays of any length.

function getMaxOfArray(numArray) {
    return Math.max.apply(null, numArray);
}

Or by using the latest spread operator, getting the maximum value in an array becomes easier.

var arr = [1, 2, 3];
var max = Math.max(...arr);

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of What is the maximum value statement in javascript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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