Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid NaN when using the Math.max() function in JavaScript?

Tags:

People also ask

Why is my function returning NaN JavaScript?

NaN , which stands for "Not a Number", is a value that JavaScript returns from certain functions and operations when the result should be a number, but the result is not defined or not representable as a number. For example: parseInt() returns NaN if parsing failed: parseInt('bad', 10) Math.

How does math MAX work in JavaScript?

The Math. max() method 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. The max() is a static method of Math, therefore, it is always used as Math.

Why is math MAX () less than math MIN ()?

max() starts with a search value of -Infinity , because any other number is going to be greater than -Infinity. Similarly, Math. min() starts with the search value of Infinity : “If no arguments are given, the result is Infinity .

What does math max method return?

max(int a, int b) returns the greater of two int values. That is, the result is the argument closer to positive infinity. If the arguments have the same value, the result is that same value.


I am trying the get the maximum value in an array of numbers:

maxtime=Math.max.apply( Math, cnttimearr );
alert(maxtime);

However, I am getting NaN instead of the maximum value. Can anyone tell me what I am doing wrong?