primitive value corresponding to a double-precision 64-bit binary format IEEE 754 value
NOTE A Number value is a member of the Number type and is a direct representation of a number.
set of all possible Number values including the special “Not-a-Number” (NaN) values, positive infinity, and negative infinity
member of the Object type that is an instance of the standard built-in Number constructor
NOTE A Number object is created by using the Number constructor in a new expression, supplying a Number value as an argument. The resulting object has an internal property whose value is the Number value. A Number object can be coerced to a Number value by calling the Number constructor as a function (15.7.1).
Noticed that “A Number object can be coerced to a Number value by calling the Number constructor as a function”. This means that regardless of the input into a new Number constructor, the input will type coerced into a number. Let’s take a look at a few examples.
var x = Number('55') //55 console.log(typeof x) //number var x = Number('asd') console.log(x) //NaN console.log(typeof x) //number
Hopefully by now you’ve figure out why the isNaN method of the Number Object also has a flaw. Remember how earlier I mentioned that Number object can be coerced to a Number value, well that’s exactly what happens when you use the isNaN function. Although the isNaN function will come back as true for both NaN and most other cases, it will still fail when that number can be coerced into an actual number. Let’s look at a few examples.
isNaN(NaN) //true isNaN("hsdjkfhd") //true isNaN("44"); //false isNaN("0o11") //false var newDate = new Date(); isNaN(newDate); //false isNaN(true); //false
If you we trying to use !isNaN() is order check whether an your value was actually a number, you can see that there would possibly several different scenarios in which that would fail. In order to be sure that you actually do have a number, you actually have to use a combination of both isNaN and typeof. Here are a couple of examples:
isNaN(true) && typeof true === "number"; //false isNaN('55') && typeof true === "55"; //false