Number Literals: Decimal, Hex, Octal, Binary
What you'll learn: How to write number values in different number systems using special prefixes in JavaScript.
The Core Idea
While we usually write numbers in decimal (base-10, like 42), JavaScript lets you express numbers in other bases too. Think of it like different languages for writing the same number — they all represent the same value, just written differently.
JavaScript provides three special prefixes:
0xfor hexadecimal (base-16)0ofor octal (base-8)0bfor binary (base-2)
Writing Numbers in Different Bases
Decimal (no prefix): The standard way you're used to:
let ten = 10;
Hexadecimal (0x prefix): Uses digits 0-9 and letters A-F:
let tenInHex = 0x0A; // Same as 10
let color = 0xFF; // Same as 255
Octal (0o prefix): Uses only digits 0-7:
let tenInOctal = 0o12; // Same as 10
Binary (0b prefix): Uses only 0 and 1:
let tenInBinary = 0b1010; // Same as 10
All the Same Value
Here's the key insight: these are just different notations for writing numbers. Once JavaScript reads them, they're all stored as the same numeric type:
console.log(0b1010); // Displays: 10
console.log(0o12); // Displays: 10
console.log(0x0A); // Displays: 10
All three log 10 because that's the decimal value they represent.
Key Takeaway: JavaScript number literals can be written in decimal (default), hexadecimal (0x), octal (0o), or binary (0b) formats — they all create the same Number type and can be used interchangeably.