點燈坊

失くすものさえない今が強くなるチャンスよ

6 Ways to Define Integer Literal

Sam Xiao's Avatar 2021-12-19

We can define integer literal in decimal, hexadecimal, octal or binary.

Version

ECMAScript 2015

Decimal Integer

let x = 100 // ?

A decimal integer literal is a sequence of digits without a leading 0.

int000

Octal Integer

let x = 0100 // ?
let y = 0o100 // ?

A octal integer literal is a sequence of digits with a leading 0 or a leading 0o.

int001

Hexadecimal Integer

let x = 0xffff // ?

A decimal integer literal is a sequence of digits with a leading 0x.

int002

Binary Integer

let x = 0b0010 // ?

A binary integer literal is a sequence of digits with a leading 0b.

int003

Conclusion

  • Be careful about leading 0. It is an octal integer, not a decimal integer