Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding underscore's implementation of isNaN

Taken from the underscore.js source:

_.isNaN = function(obj) {
  return _.isNumber(obj) && obj != +obj;
};

Why did they do it this way? Is the above implementation equivalent to:

_.isNaN = function(obj) {
  return obj !== obj;
};

If it is, why the "more complicated" version? If it is not, what are the behavioural differences?

like image 673
Randomblue Avatar asked Mar 02 '13 15:03

Randomblue


People also ask

What is the purpose of isNaN?

The isNaN() function is used to check whether a given value is an illegal number or not. It returns true if value is a NaN else returns false. It is different from the Number. isNaN() Method.

How do I know my isNaN?

1. isNaN() Method: To determine whether a number is NaN, we can use the isNaN() function. It is a boolean function that returns true if a number is NaN otherwise returns false.

What value is returned by the isNaN () method?

isNaN() method returns true if the value is NaN , and the type is a Number.

What is the difference between isNaN and number isNaN?

isNaN converts the argument to a Number and returns true if the resulting value is NaN . Number. isNaN does not convert the argument; it returns true when the argument is a Number and is NaN .


1 Answers

_.isNaN(new Number(NaN)) returns true.

And that's by design.

var n = new Number(NaN);
console.log(_.isNaN(n), n!==n); // logs true, false
like image 71
Denys Séguret Avatar answered Sep 29 '22 07:09

Denys Séguret