SyntaxStudy
Sign Up
JavaScript Number Formats: Binary, Hex, Octal, Scientific
JavaScript Intermediate 7 min read

Number Formats: Binary, Hex, Octal, Scientific

Alternative Number Formats in JavaScript

JavaScript supports numeric literals in four bases — decimal, binary, octal, and hexadecimal — as well as scientific (exponential) notation. These are useful when working with bitwise operations, colours, file permissions, or very large or very small quantities.

Hexadecimal (base 16)

Prefix with 0x or 0X. Widely used for colour codes, memory addresses, and Unicode code points.

Binary (base 2)

Prefix with 0b or 0B. Essential for bit manipulation and understanding flags.

Octal (base 8)

Prefix with 0o or 0O. Occasionally used for Unix file permission masks.

Scientific Notation

Write 1e6 for one million or 1.5e-3 for 0.0015. The exponent indicates a power of ten.

Number.prototype.toString(radix)

Convert a number to a string in any base between 2 and 36 using num.toString(base).

Example
console.log(0xFF);       // 255 (hex)
console.log(0b1010);     // 10  (binary)
console.log(0o17);       // 15  (octal)
console.log(1e6);        // 1000000
console.log(1.5e-3);     // 0.0015
console.log((255).toString(16)); // 'ff'
console.log((10).toString(2));   // '1010'
console.log((15).toString(8));   // '17'
const color = 0xFF5733;
console.log(color.toString(16)); // 'ff5733'
Pro Tip

Use numeric separators for readability in large literals: 1_000_000 is identical to 1000000 and far easier to scan at a glance — this ES2021 feature is supported in all modern environments.