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.
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,...)
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>
Execution results As follows:
Output : 32 Output : -1
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>
The execution result is as follows
Output : -Infinity
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>
The execution result is as follows
Output : NaN
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!