IsNaN function in nodejs

why is nodeNs isNaN function returns false in the following cases?

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

It is very strange.

Does anyone have any ideas, as I thought isNaN means "Not a Number".

someone can clarify

Thanks in advance!

+6
source share
4 answers

Since you are not passing a number to it, it will convert it to a number. They all convert to 0 , which is 0 , not NaN

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

Note that NaN does not mean "not a JavaScript number." In fact, it is completely separate from JavaScript and exists in all languages ​​that support IEEE-754 floats.

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

 if (typeof value === "number") { } 
+11
source

NaN is a very specific thing: it is a floating point value that has the corresponding NaN flags set in the IEEE754 specification ( Wikipedia article ).

If you want to check if a string has a numerical value in it, you can do parseFloat(str) ( MDN on parseFloat ). If this fails to find valid numeric content or to find invalid characters before searching for numbers, it will return the value NaN.

So, try making isNaN(parseFloat(str)) - it gives me true for all three published examples.

+3
source

isNan() designed to detect things that are "mathematically undefined" - for example. 0/0 -

 node > isNaN(0/0) true > isNaN(1/0) false > isNaN(Math.sqrt(-1)) true > isNaN(Math.log(-1)) true 

The other piece of advice you got here on this question on how to determine numbers is solid.

+3
source

isNaN - this function is not a number, it returns true when the number is not specified as a parameter, and false when the number is specified

0
source

Source: https://habr.com/ru/post/970438/


All Articles