Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to write the maximum statement in javascript

Shulou Source: shulou.com Published: 2022-05-31 11:45:16 09月25日 Update

This article introduces the relevant knowledge of "how to write the maximum sentence in javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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

This article operating environment: windows7 system, javascript1.8.5 version, Dell G3 computer.

What is the maximum statement in javascript?

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

Grammar

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

Parameters.

Value1, value2,...

A set of values

Return value

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

Description

Because max is a static method of Math, you should use Math.max () rather than the 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

Use Math.max ()

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

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

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

Or it is easier to get the maximum value in the array by using the latest extension statement spread operator.

Var arr = [1, 2, 3]; var max = Math.max (.. arr); "how to write a maximum statement in javascript" is introduced here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Maximum maximum statement parameter method function numerical value array action content number more knowledge result practicality achievement and then element dilemma example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei macOS OPPO Reno Redmi Xiaomi