Search code examples
javascriptcoercion

Javascript - Why isNaN(1 + null) returns false?


Well,I am going through Mozilla Javascript Reference and found that..

isNaN(1 + null) //false
isNaN(1 + undefined) //true

I am not able to understand reason behind this.


Solution

  • From https://www.w3schools.com/js/js_type_conversion.asp, , when null is converted to number, it becomes 0. Therefore, 0 + 1 = 1, it's valid number, so isNaN returns false.

    When undefined is converted to Number, it becomes NaN. Any Number + NaN = NaN, so isNaN return true.