Skip to content

Number Bases

A number base, or radix, is how many different digits a counting system uses before it carries into the next column. Everyday numbers are base 10, so each column is worth ten times the one to its right. Computers work in base 2 (binary), and programmers often write binary values in base 16 (hexadecimal) or, less often, base 8 (octal), because each hexadecimal digit stands for exactly four bits and each octal digit for three. JavaScript can write numbers in all of these and convert between any bases from 2 to 36.

In base 10, the number 2026 means 2 thousands, 0 hundreds, 2 tens and 6 units. In any base the columns work the same way, except that each is worth the base times the column to its right:

Base Digits used Column values from the right 12 written in this base
2 (binary) 0 and 1 1, 2, 4, 8, 16 1100
8 (octal) 0 to 7 1, 8, 64, 512 14
10 (decimal) 0 to 9 1, 10, 100, 1000 12
16 (hexadecimal) 0 to 9, then a to f for 10 to 15 1, 16, 256, 4096 c

So binary 1100 is 8 + 4, octal 14 is 8 + 4, and hexadecimal c is 12 on its own.

A prefix tells JavaScript which base a number literal is in: 0b for binary, 0o for octal and 0x for hexadecimal. The letters can be upper or lower case. The value is the same number whichever way it is written:

const dozen = 12;
console.log(0b1100);
console.log(0o14);
console.log(0xc);
console.log(dozen === 0b1100 && dozen === 0o14 && dozen === 0xc);

This prints:

12
12
12
true

A number does not remember the base it was written in, so console.log always shows it in decimal.

toString(radix) returns the digits of a number in the given base, as a string:

const year = 2026;
console.log(year.toString(2));
console.log(year.toString(8));
console.log(year.toString(16));
console.log(year.toString(36));
console.log((255).toString(16).toUpperCase());

This prints:

11111101010
3752
7ea
1ka
FF

Base 36 uses all ten digits and all 26 letters, which makes it the most compact base JavaScript supports.

parseInt(text, radix) goes the other way, reading a string in the given base and returning a number. Number() also understands the 0b, 0o and 0x prefixes in a string, but it takes no radix argument:

console.log(parseInt('11111101010', 2));
console.log(parseInt('7ea', 16));
console.log(Number('0x7ea'));
console.log(Number('0b11'));

This prints:

2026
2026
2026
3

ParseInt and ParseFloat covers how parseInt handles text that is not a valid number.

Hexadecimal is how CSS writes colours, with two digits for each of red, green and blue. padStart keeps a small value such as 8 to two digits. Base 36 turns a long number, such as a timestamp, into a short string that can be read back exactly:

const rgb = [42, 157, 8];
const hex = '#' + rgb.map((n) => n.toString(16).padStart(2, '0')).join('');
console.log(hex);
const id = 1745930412881;
console.log(id.toString(36));
console.log(parseInt(id.toString(36), 36));

This prints:

#2a9d08
ma2hw0gh
1745930412881

Binary is most useful with the bitwise operators, which are covered in Bitwise Operators & Binary Numbers.

Steve Griffith covers this topic in his video Number Bases for Non-Math Lovers.