azero0 - 4 months ago 17

Node.js Question

why is isNaN function in nodejs returning **false** in the following cases?

`isNaN(''),isNaN('\n'),isNaN('\t')`

this is very weird.

does somebody have any ideas as I thought isNaN stood for is Not a Number.

can someone please clarify

Thanks in advance!

Answer

Because you are not passing it a number, it will convert it to number. All of those convert to `0`

which is `0`

and not `NaN`

```
Number('')
0
Number('\n')
0
Number('\t')
0
isNaN(0)
false
```

Note that `NaN`

does not stand for "not a JavaScript Number". In fact it's completely separate from JavaScript and exists in all languages that support IEEE-754 floats.

If you want to check if something is a javascript number, the check is

```
if (typeof value === "number") {
}
```

Source (Stackoverflow)

Comments