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 1 year has passed since last update.

JavaScript:0で始まる数値の扱い

Posted at

JavaScriptで0で始まる数値を扱う際、ハマったので書き残す。

  • 以下は期待通り
let $num1 = 0800
let $num2 = '0800'
console.log($num1 === Number($num2)) // true
  • 以下が想定外
let $num3 = 0700
let $num4 = '0700'
console.log($num3 === Number($num4)) // false
console.log(Number($num3)) // 448
console.log(Number($num4)) // 700

なぜ?

0(ゼロ)で始まる数字は、後に続く数字がすべて 0~7 であれば 8進数として、8~9 を含んでいれば10進数として解釈されます。
https://www.tohoho-web.com/js/number.htm

そうなんだ、、、

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