Numeric Literal Formats
Version: ES2015 (octal, binary), ES2020 (BigInt)
Level: Beginner
There are a number of different ways to represent numbers in JavaScript source code, whether it be integer values, decimal values, or even hexadecimal. In fact, as of ES2020, there are now also two different kinds of number types in the language. Not only do you have the classic and [questionably] dependable Number type, but now, there’s the new, massive BigInt number type. Both have their own, though very similar, literal syntax. For the Number type:
// Number literals
123 // integer
1.23 // decimal
0xDEF // hexadecimal
0o077 // octal
0b101 // binary
1.23e+5 // exponential notation
BigInt literals are similar to Number literals except they include an n
suffix. BigInts also don’t support decimals or exponential syntax.
// BigInt literals
123n // integer
0xDEFn // hexadecimal
0o077n // octal
0b101n // binary
Note: You may also be familiar with an older octal style in which numbers starting with just a 0
, rather than 0o
, were seen as octal values. This format is considered deprecated, and in fact not allowed in strict mode, and should be avoided.
But wait! There’s more! Looking ahead to the future of JavaScript, you may see yet more ways to represent number values, both with new types and changes to the existing literal syntax.
// The future
1_000_000 // numeric separators (= 1000000)
1.23m // BigDecimal (decimal version of BigInt)
123@px // decorators (extended literals)
In fact, numeric separators are a stage 3 proposal and already available in most browsers today.
More info: