Which checks the expression 'v! == v '?

I saw this in the sources of one library and am confused. I think he always appreciates " false". What is the point of using this?

+5
source share
2 answers

It checks to see if v NaN:

if( v !== v ){
    //'v' is NaN   here
}

From standard :

A reliable way for ECMAScript to check if an X NaN value is an expression of the form X! == X. The result will be true if and only if X is NaN.

Why not just use the built-in isNaN()?

The answer is simple: " not reliable enough. ". Here are cases when there will be a failure: isNaN()isNaN()

isNaN("NaN")         //true
isNaN(undefined)     //true
+6
source

"Not A Number": NaN !== NaN equals true

MDN:

NaN Not-A-Number - , Number.NaN. NaN , . , .

NaN . Math (Math.sqrt(-1)) (parseInt("blabla")).

+1

All Articles