2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

NaN === NaN はfalseで判定される

Posted at

条件式で

if (Number(var) !== NaN) {
...

のように書いて、varが数字文字列(例: '1')だった時の処理をif文内で行おうしたら、なぜかNumber(var) !== NaNtrue判定されてしまい、想定通りの処理ができなかった。

調査したところ、どうやらNaN === NaNfalse判定される仕様らしい。この場合、

if (!isNaN(Number(var)) {
...

で想定通り動くようになった。が、今度はeslintに

Unexpected use of 'isNaN'

と怒られる。

if (!Number.isNaN(Number(var))) {
...

これでeslint先生のお叱りも回避できた。よかったよかった。

参考記事

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?