Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write the maximum statement in javascript

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report