Home > Web Front-end > JS Tutorial > How to use max function

How to use max function

不言
Release: 2021-04-16 14:12:45
Original
21285 people have browsed it

The max function can be used to return the largest value among multiple numbers. The syntax of this function is "Math.max(value1, value2,...)", where if no parameters are passed, the result is "-Infinity", if at least one argument cannot be converted to a number, the result is NaN.

How to use max function

The operating environment of this article: Windows 7 system, Dell G3 computer, javascript1.8.5.

The max function can be used to return the largest value among multiple numbers. If no parameters are passed, the result is "-Infinity". If at least one parameter cannot be converted to a number, the result is NaN. Let's take a look at the specific use of the max function.

Let’s first look at the basic syntax of the max function

Math.max(value1,value2,...)
Copy after login

Value1, Value2,...: The values ​​passed to the math.max() function are used to find the maximum value.

Let’s look at specific examples

When the parameters are positive and negative numbers:

The code is as follows

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<script type="text/javascript"> 
  document.write("Output : " + Math.max(10, 32, 2)); 
  document.write("Output : " + Math.max(-10, -32, -1));        
</script> 
</body>
</html>
Copy after login

Execution results As follows:

Output : 32
Output : -1
Copy after login

When no parameters are passed

The code is as follows:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<script type="text/javascript"> 
   document.write("Output : " + Math.max());       
</script> 
</body>
</html>
Copy after login

The execution result is as follows

Output : -Infinity
Copy after login

When there are parameters in the parameters that cannot be converted to numbers:

The code is as follows

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body>
<script type="text/javascript"> 
   document.write("Output : " + Math.max(10,2,NaN));       
</script> 
</body>
</html>
Copy after login

The execution result is as follows

Output : NaN
Copy after login

This article ends here It's all over. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !

The above is the detailed content of How to use max function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template