Using Math max() function to coding in Javascript/JS

This JavaScript tutorial explains how to use the math function known as max() with syntax and examples.


In JavaScript, max() is a characteristic that is used to return the biggest cost from the numbers supplied as parameters. Because the max() characteristic is a static function of the Math object, it need to be invoked through the placeholder object known as Math.


In JavaScript, the syntax for the max() function is:

Math.max([number1, number2, ... number_n]);

Parameters or Arguments

number1, number2, … number_n Optional. The numbers that will be evaluated to decide the biggest value.


The max() feature returns the largest price from the numbers provided.

If no parameters are provided, the max() feature will return -Infinity.

If any of the parameters supplied are not numbers, the max() feature will return NaN.


Math is a placeholder object that includes mathematical functions and constants of which max() is one of these functions.


Let’s take a appear at an example of how to use the max() feature in JavaScript.

For example:

console.log(Math.max(10, 20));
console.log(Math.max(10, 20, 30));
console.log(Math.max(-2, -4, -6, -8));

In this example, we have invoked the max() function using the Math class.

We have written the output of the max() characteristic to the net browser console log, for demonstration purposes, to exhibit what the max() characteristic returns.

The following will be output to the net browser console log:


In this example, the first output to the console log returned 20 which is the largest of the values 10 and 20.

The 2nd output to the console log returned 30 which is the greatest of the values 10, 20 and 30.

The 0.33 output to the console log back -2 which is the biggest of the values -2, -4, -6 and -8.

Using the max() characteristic to discover the Largest Value in an Array

Finally, we’ll take a seem to be at how to use the max() function to locate the largest cost in an array.

For example:

var totn_array = [5, 10, 15, 20, 25];


The following will be output to the net browser console log:


In this example, the max() approach returned 25 which is the greatest fee from the array known as totn_array. This array consisted of 5 elements with the numeric values: 5, 10, 15, 20 and 25.