Saturday, November 9, 2024

JavaScript NaN and undefined

NaN is a special number in JavaScript which is not equal to any number including itself. Using any comparison operator such as >, <, >= , <=, ==, != will return false if one of operands (or both operands are) is NaN. 

In case of comparison operator, if one of the operands is undefined then it is converted into NaN but undefined is loosely equal to itself.

When comparing NaN with any other value using these operators, the result is always false. This is because NaN is not considered greater than or less than any value, including itself.

When using comparison operators with undefined, JavaScript converts undefined to NaN (Not-a-Number).
In case of comparison, undefined and NaN are same.

Notes
  • undefined == undefined is true but NaN == NaN is false.
  • undefined == null is true
  • undefined == Any other value is false
  • undefined is loosely never equal to any other operand except null.

No comments:

Post a Comment

Hot Topics