Math Max Javascript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

Overview

In javascript, max() is a function of the Math object, which is used to find the maximum of numbers given as parameters.

Syntax of Math.max() in Javascript

The syntax of the max() function is not so detailed; it can be accessed by the Math object.

Although there is no specific restriction to the number of parameters that can be passed to this max function, we need to consider the interpreter on which our code will be executed because each interpreter has a different limit regarding the number of parameters that can be passed during any function call.

Parameters of Math.max() in Javascript

This function just asks for 0 or more parameters to compare among them.

The type of parameter should be either number or convertible to a number; otherwise max() function will not work properly.

  • The term convertible denotes that if the parameter passed can represent any number, then regardless of type, it will be converted to a number by javascript's ToNumber operation.
  • For instance:
    • boolean false will be converted to 0.
    • string will be converted to a number if the string represents a number, ex:- "15" will be converted to 15.
    • null will be converted to 0.

Return value of Math.max() in Javascript

The return type depends upon the type and number of parameters being passed,

  • -Infinity: If no parameters are passed.
  • NaN: If any provided parameter is not a number as well as cannot be converted to a number via implicit conversion.
  • Largest Value: If parameters are provided correctly, the function will return the largest value among them.

Example :

In this example, we will test the working of the Math.max() function by providing different parameters.

The return value of the function will be then printed by the console.log() statement.

Output:

What is Math.max() in Javascript and how does it Work ?

In javascript, there is a global Math object which contains some functions by which we can perform some mathematical operations, and max() is one of those functions.

It is a static function of the Math object, so there is no need to create an object before the call to this method.

This function works by accepting the 1 or more parameter, and it returns the maximum among those parameters. It will not work properly if the parameters are not provided in an expected manner.

More Examples

Parameters in a Twisted Manner

In this example, we will not provide some basic parameters, but rather some parameters in a twisted manner to demonstrate that the function will still operate if the parameter finally resolves to a number.

Output:

Explanation:

  • In javascript, the functions 'parseFloat ()andparseInt()` return a number if the string passed as a parameter represents a number.
  • Similarly .length property of string and array provides the length of string, which is 10 in the case of Javascript and 3 in the case of array [10, "a", 78.35].
  • So from here, we can see that if ultimately the parameters are reducing to some number, then this function can work properly.

Inappropriate Parameters

Let's see what will happen if we provide inappropriate parameters to this function.

Output :

Explanation :

  • In the first call to the max() function, no parameters were provided; hence, -Infinity was returned.
  • In the second function call, we have provided the string "Hello" which is not a number hence NaN was returned.

Find the Maximum in an Array

If we have our array and we want to find the maximum element of that array, we can use a reduced higher-order function along with Math.max() to achieve the solution to this problem.

The reduce function accepts a callback and the initial value, the callback function provides access to a variable named previousValue, which is initialized with that provided initial value, and we can change it while executing the function, also further the callback function iterates on the caller array and the value we return from callback function is assigned to previousValue, the variable which was earlier initialized with the initial value.

Output :

Explanation :

  • We are providing initialValue as -Infinity to the reduce function.
  • At the first iteration, the provided callback returns the Maximum of previousValue(which is -Infinity) and the currentElementOfArray(which is 38).
  • The returned value will be assigned to previousValue.
  • Now, in the next iteration comparison will be done between 38 and 15. Again the 38 will be returned.
  • In this way, the reduce function will return the maximum of the entire array.

Supported Browsers

This method is supported by all commonly used browsers,

Conclusion

  • max() function belongs to the Math object of Javascript.
  • It finds out the maximum of the provided numbers. The parameters could be 0 or more.
  • It returns -Infinity if no parameters are provided.
  • It returns NaN if one of the parameters is not a number as well as not convertible to a number.