Tutorialspoint.dev

Math.max() In JavaScript

The Math.max() function is used to return the largest of zero or more numbers.
The result is “-Infinity” if no arguments are passed and the result is NaN if at least one of the arguments cannot be converted to a number.
max() is a static method of Math, therefore, it is always used as Math.max(), rather than as a method of a Math object created.

Syntax:

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

Parameters Used:
Value1,Value2 :Values sent to math.max() function for finding the largest.

Return Value:
The Math.max() function returns the largest of the given numbers.

Examples:



Input : Math.max(10, 32, 2)
Output : 32
     
Input : Math.max(-10, -32, -1)
Output : -1

Input : Math.max()
Output : -Infinity


Input : Math.max(10,2,NaN)
Output : Nan
  1. When positive numbers are passed as parameters.
    <script type="text/javascript">
       document.write("Output : " + Math.max(10, 32, 2));
    </script>

    Output:

    Output : 32
  2. When negative numbers are passed as parameters.
    <script type="text/javascript">
       document.write("Output : " + Math.max(-10, -32, -1));
    </script>

    Output:

    Output : -1
  3. When no parameters are passed.
    <script type="text/javascript">
       document.write("Output : " + Math.max());
    </script>

    Output:

    Output : -Infinity
  4. When NaN is passed as a parameter.
    <script type="text/javascript">
       document.write("Output : " + Math.max(10,2,NaN));
    </script>

    Output:

    Output : NaN


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter