I’ve recently written blogs on both JavaScript’s Arrays and Strings, in order to keep that theme going today we’ll take a look at JavaScript’s Numbers. In this post, I will be referring to JavaScript’s Number object by Number, and to the data type as number. Now why would we take the time to go over the differences between JavaScript’s Number object and its number data type. The fact is that a number type by itself doesn’t have any JavaScript properties. All the properties that you use to interact with numbers inside JavaScript actually come from JavaScript’s Number object. In order to better understand what JavaScript’s number type actually is let’s refer to EcmaScript standard, chapter 8.5 The Number Type:

Number value

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.

Number type

set of all possible Number values including the special “Not-a-Number” (NaN) values, positive infinity, and negative infinity

As you can see NaN is actually part of JavaScript’s number data type. This is the reason why when you use typeof NaN you get “Number”. As we’ve seen with Arrays, the typeof operator can’t always be trusted. Also like the Array Object the Array number has its own Method in order to determine if the item is not a number, and that is the isNaN function. The issue with has NaN is that it also has a flaw. But before we get to that let’s first take a look at what the Number Object actually is, and for that we’ll again using Ecmascript standard:

Number Object

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
```

I hope this post has helped you realized the importance of being careful when working with the Number object in JavaScript. Make sure to always keep in mind that you are working with a Number Object and not the number data type, so always keep in mind that the data you are working with could have been type coerced.

Paulo Diniz