AppDividend
Latest Code Tutorials

JavaScript Math max() Function | max() in Javascript

0

JavaScript Math max() is an inbuilt function that is used to find the maximum out of a set of numbers. The max() method comes handy in the most basic of programs as well as in programs dealing with advanced algorithms. JavaScript Math.max() method initially compares with negative infinity, and that is why if no argument is passed, this method returns negative infinity.

JavaScript Math max()

Javascript max() function returns the number with the highest value. This function accepts Value1,Value2 Values sent to math.max() function for finding the largest.

Syntax

Math.max(x, y, z, ...)   // can pass any number of arguments

Parameters

The numbers out of which the maximum is to be determined.

Return Value

The maximum of all the parameters passed.

See the following figure.

JavaScript Math max()

 

Note

  1. If no argument is passed, this method returns negative infinity.
  2. If any of the arguments cannot be converted into a valid number, this method returns NaN.
  3. If the parameter is null, the max() method returns 0.
  4. If a parameter is an empty string, the max() method returns 0.
  5. If the parameter is an empty array, the max() method returns 0.
  6. If one argument is positive zero, and the other argument is negative zero, this method returns positive zero as it considers negative zero to be strictly smaller than positive zero.

Compatibility(Version and above)

  1. Google Chrome v1
  2. Internet Explorer v3
  3. Firefox v1
  4. Edge v12
  5. Opera  
  6. Safari v1
  7. Android webview v1 
  8. Chrome for Android v18
  9. Firefox for Android v4
  10. Opera for Android 
  11. Safari on iOS v1
  12. Samsung Internet v1.0
  13. Node.js

JavaScript version: ECMAScript 1

Consider the following examples.

example1.js

The following example demonstrates the use of the js max() method.

// example1.js

console.log(Math.max(1, 2, 3));
console.log(Math.max(-1, 2, 3, -6));
console.log(Math.max(0, 0, -3));
console.log(Math.max(-1, -2));
console.log(Math.max(-5, -2, -3, 1));

Output

node example1
3
3
0
-1
1

example2.js

The following example demonstrates the case no argument is passed.

//example2.js

console.log(Math.max());

Output

node example2
-Infinity

example3.js

The following example demonstrates the cases where NaN is returned and cases where it can be avoided.

//example3.js

var a = "JavaScript"; 	       // non-numeric string
var b = [1, 2, 3, 4]; 	       // array with more than one element
var c;			       // undefined variable
var d = {};		       // empty object

console.log(Math.max(a, 1));
console.log(Math.max(b, 2));
console.log(Math.max(c, 3));
console.log(Math.max(d, 4));

var e = "23";  			// numeric string
var f = [10]; 			// array with a single element

console.log(Math.max(e, 5));
console.log(Math.max(f, 1));

Output

node example3
NaN
NaN
NaN
NaN
23
10

example4.js

The following example demonstrates the cases where 0 is returned.

// example4.js

var a = null;
var b = "";
var c = [];

console.log(Math.max(a, -1));
console.log(Math.max(b, -2));
console.log(Math.max(c, -3));

Output

node example4
0
0
0

example5.js

The following example demonstrates the case where the maximum out of positive zero and negative zero is to be determined.

// example5.js

var a = 0;
var b = -0;

console.log(Math.max(a, b));

Output

node example5
0

Finding the maximum element of an array in Javascript

The max() method is not designed to handle an array with multiple items being passed as a parameter directly.  Consider the following example:

example6.js

The following example demonstrates an array with various elements being passed as a parameter directly.

// example6.js

var arr = [1, 2, 3, 4];
console.log(Math.max(arr));

Output

node example6
NaN

However, the max() method can be used to find the maximum of an array of elements in the following ways:

  1. Using a Javascript array.reduce() method.
  2. Using the Function.prototype.apply() method.
  3. Using the spread operator.

Using Array.reduce() method

To find the maximum element in an array, the Array.reduce() method can be used to compare all the elements of the Array.

var arr = [1, 2, 3, 4];
var max_element = arr.reduce(function (x, y) {
  return Math.max(x, y);
});

Consider the following example.

example7.js

The following example demonstrates the use of the Array.reduce() method with max() to find the maximum element of an array.

// example7.js

var arr = [34, 54, 0, -70, 64];
var max_element = arr.reduce(function (x, y) {
  return Math.max(x, y);
});

console.log(max_element);

Output

node example7
64

Using Function.prototype.apply() method

Using Function.prototype.apply(), one can find the maximum element of an array. However, this must be used on arrays having only a few numbers of elements.

function findMax(arr){
	return Math.max.apply(null, arr);
}

Consider the following example.

example8.js

The following example demonstrates the use of the Function.prototype.apply() method with max() to find the maximum element of an array.

// example8.js

function findMax(arr) {
  return Math.max.apply(null, arr);
}

var new_arr = [45, 67, -92, 100];

console.log(findMax(new_arr));

Output

node example8
100

Using the spread operator

The above apply() method can be implemented with the help of the spread operator (…) as well. Again, this must be used on arrays having only a few numbers of elements.

var max = Math.max(…arr); 

Consider the following example.

example9.js

The following example demonstrates the use of the spread operator with max() to find the maximum element of an array.

// example9.js

var arr=[10, 20, -90, 6];
console.log(Math.max(...arr));

Output

node example9
20

See also

Javascript Math.floor()

Javascript Math.random()

Javascript Math.cbrt()

Javascript Math.exp()

Javascript Math.log()

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.