# Javascript max()

In javascript max() function is a static javascript function that is used to return the largest number from two numbers provided as parameters. The max() function can be invoked through the placeholder object called Math and must always have at least one parameter. If there are more than two parameters, then all but one of them will be ignored and only the last parameter will be used in the calculation.

## How Javascript max function works:

The javascript Math.max() method can be used to find maximum values of numbers provided as parameters by max(). This clever little piece of code returns undefined if any value is NaN or missing, making it super handy for determining max values across platforms with various data types! Let’s see how it work below.

Syntax:

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

Example:

``````<script type="text/javascript">
document.write(Math.max(10, 32, 2));
</script>``````

This will print out the biggest number from the three numbers provided as parameters which are 32.

Parameters:

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

This method takes one single parameter that can be used as many times or few times. Let’s take a look at the details of how this works below:

• Value: The values are sent to math.max() method for finding the largest, which returns a no that is largest from all others.

Return Type: The Math.max() method returns an integer that is the largest of given numbers, which means it always returns a value greater than all others in its scope.

## What happens when passed a Negative number:

If passed a negative number, javascript math.max() returns the smallest number.

## What happens when passed NaN or missing value:

If javascript math.max() is given a number that doesn’t exist, it returns undefined. If math. max() is given the parameter where there are no numbers available at all, then javascript returns -Infinity.

Now let’s take an example to understand how javascript works with negative and missings values:

Example 1:

``````var a = -200;
console.log(Math.max(-a, 200));
// prints '200' because it's the largest number that is available in this case.``````

Example 2:

``````var b = 100;
console.log(Math.max(b, undefined));
// prints '100' because it's the largest number that is available in this case.``````

Example 3:

``````var c = 100;
console.log(Math.max(-200,c));
// prints '-200' because javascript math.max() function returns -Infinity when there are no numbers provided as parameters.``````

Example 4:

``````console.log(Math.max(c, -200));
// prints '-100' because javascript MATH.max() function returns the smallest number when negative values are passed on to it.``````

## How javascript max() works with array:

The javascript Math.max() method can also accept an Array as a parameter to compare the elements of that given array and find the maximum value in it. Let’s see how this is done below:

``````var arr = [34, -23, 100];
console.log(Math.max(arr)); // prints 100``````

### The following are the steps that are followed by javascript for this javascript code:

1. Javascript will first convert the given array to numeric values so that the javascript Math.max() function can work on them as numbers and not a string.
2. The javascript MATH.max() method then calculates the maximum value between all of these converted elements, which is 100 in this case.
3. Javascript returns the maximum value between all elements, which is 100 in this case.

## How javascript max() works with strings:

The javascript max() method can also accept the String as a parameter to compare the String and return NAN when compared using this javascript function. Let’s see how it is done below:

``````var string = "hello";
console.log(Math.max(string));
// prints 'NAN' because it's not a number that is available in this case.``````

## Conclusion:

The max() function is a static JavaScript function that takes two arguments, x and y. If these are both numbers this will return the greater of the two values passed into it. It can be used to compare strings as well but if you have text with mixed cases or symbols then it won’t work. This article gives an overview of what max() does so you know how best to use it when needed!