3
1

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.

JavaScriptのNumber() 関数で文字列やnullを数値に変換した結果

Last updated at Posted at 2020-04-11

JavaScriptのNumber() 関数とは

プリミティブ型オブジェクトのNumberを生成する

変換した結果

console.log(Number('123'))       // 123
console.log(Number('12.3'))      // 12.3
console.log(Number('12.00'))     // 12
console.log(Number('123e-1'))    // 12.3
console.log(Number(''))          // 0
console.log(Number(null))        // 0
console.log(Number('0x11'))      // 17
console.log(Number('0b11'))      // 3
console.log(Number('0o11'))      // 9
console.log(Number('foo'))       // NaN
console.log(Number('100a'))      // NaN
console.log(Number('-Infinity')) //-Infinity
console.log(Number(undefined))   // NaN

NaN(Not a Number)について

NaNは数値ではないことを示します。

注意点

Number('')やNumber(null)は、NaNではなく0を返すので注意が必要です。
ただNumber(undefined)はNaNを返します。
私は一度ハマりました…。

参考サイト

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?