0
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 3 years have passed since last update.

NaNはNumber型

Posted at

tl;dr

NaN(Number.NaN)は数値ではありませんがNumber型です。

説明

数値、またはNaNの変数は以下のように定義します。

const foo: number = 123;
const foo: number = Number.NaN;

以下のような定義はできません。

const foo: number | NaN = Number.NaN;

NaNのチェックにはisNaN()を使います。

const foo: number = parseInt('a');
const bar: number = Number('a');
console.log('foo', foo, isNaN(foo));
console.log('bar', bar, isNaN(bar));

実行結果

foo NaN true
bar NaN true

単純に真偽値でチェックすると0との区別がつかないので注意!

const foo: number = parseInt('a');
const bar: number = parseInt('0');
console.log('foo', foo, !foo);
console.log('bar', bar, !bar);

実行結果

foo NaN true
bar 0 true

以上。

0
0
0

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
0
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?