Number Literals

Binary, Octal and Hexadecimal Literals; Numeric Separators

Integer literals can be written in four bases: 0b binary and 0o octal (both ES2015), 0x hexadecimal, and decimal. They are only notations: 0xff and 255 are the same number. ES2021 added the underscore as a numeric separator to group digits; it works in every base, in fractions, exponents and BigInt literals, and is ignored by the engine. It may appear only between two digits, so 1__0, 1_ and 0_1 are all SyntaxErrors.

Number bases and digit separatorsJavaScriptLive
console.log(0b1010, 0o17, 0xff, 1_000_000, 0b1111_0000, .5e-2);
console.log((255).toString(2), Number("0b101"), parseInt("0o17"), 0xff_ff);

In strings, Number() understands all three prefixes but parseInt() only 0x, and neither accepts separators (Number("1_000") is NaN). The legacy octal 010 (value 8) is a SyntaxError in strict mode and modules; write 0o10. Binary literals with separators make bit flags readable (Bitwise Operators).