Javascript NaN | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Javascript NaN

null==undefined //true isNaN(null) //false isNaN(undefined) //true NaN==undefined //false Can you please explain this?i cannot get it

23rd May 2020, 11:53 AM
Lalis
Lalis - avatar
4 Answers
+ 1
JavaScript has both strict and type–converting comparisons. A strict comparison (e.g., === ) is only true if the operands are of the same type and the contents match. The more commonly-used abstract comparison (e.g. == ) converts the operands to the same type before making the comparison. You should use === strict comparision For example: NaN === undefined # False Null === undefined # False NaN === Null # False But: NaN === NaN # False NaN is special in that it doesn't have a real value, so comparing it to itself doesn't return true. Essentially, NaN is equal to nothing, not even NaN . ... NaN is the same as boolean false . Of course false === false , so the expression evaluates to true.
23rd May 2020, 1:34 PM
Muhammadamin
Muhammadamin - avatar
+ 1
You may consider it easy : isNaN(null); // false Whenever you pass a nonnumerical value as the argument of the isNaN method, it tries to convert it to a number. After conversation, if it evaluates to a number, then returns false. Otherwise returns true. console.log(+null); // 0 A little '+' in front of anything (i.e string) makes it a number. As you can see, the conversation of null to a number is 0. And isNaN(0) is false. Therefore the final answer is false. However, console.log(+undefined); // NaN You can see after the conversation, it gives us NaN. And isNaN(NaN) is of course true. The other two were explained by Muhammad :))
23rd May 2020, 1:43 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
0
Arb Rahim Badsa Muhammad when converting both null and undefined using == give same value.but why converting using isNaN give different value or how it compare other with NaN value.while converting null to number gives 0 then why not converting undefined to number gives 0 when they have same content(null==undefined)
23rd May 2020, 4:36 PM
Lalis
Lalis - avatar
0
To solve those misconceptions we have Number.isNaN static method comes with EcmaScript 2015# Number.isNaN Number.isNaN(undefined) #false IsNaN() differs from Number.isNaN()
23rd May 2020, 5:48 PM
Muhammadamin
Muhammadamin - avatar