2020年1月22日水曜日

Javascript NaN, comparing or not

NaN is not a number and would appear when you calculate number.

>const result = "foo" * 2;

it will give you NaN as result.

however this

>console.log(result === NaN);

it will print false

because

NaN compares unequal (via ==, !=, ===, and !==) to any other value -- including to another NaN value. Use Number.isNaN() or isNaN() to most clearly determine whether a value is NaN. Or perform a self-comparison: NaN, and only NaN, will compare unequal to itself.


>Number.isNaN(result)
>isNaN(result)

will return true


0 件のコメント:

コメントを投稿