Sub topic 2
Writing Numbers: Decimal, Hex, Octal, Binary, Exponents, and Separators
This chapter assumes you know basic JavaScript syntax, can run code in a browser console or Node.js, and have read the previous chapter. That chapter explained that every JavaScript number is a 64-bit IEEE 754 double.
A numeric literal is a number written directly in source code. Literals come in several notations, but every notation produces the same kind of value. 0xff and 255 are the same double. The notation only changes how you write the number. It does not change what gets stored.
The Four Bases
A prefix tells the parser which base the digits use. The prefix letters are case-insensitive, but the lowercase forms are the usual style.
| Notation | Prefix | Digits allowed | Example | Value |
|---|---|---|---|---|
| Decimal | none | 0–9 | `255` | 255 |
| Hexadecimal | `0x` | 0–9, a–f | `0xff` | 255 |
| Octal | `0o` | 0–7 | `0o377` | 255 |
| Binary | `0b` | 0–1 | `0b11111111` | 255 |
console.log(255, 0xff, 0o377, 0b11111111); // 255 255 255 255
console.log(0xFF === 255); // true
console.log(1e3); // 1000
console.log(2.5e-4); // 0.00025
console.log(1e21); // 1e+21
console.log(.5 + 5.); // 5.5The console always prints numbers in decimal, whatever notation you used to write them.
Exponent Notation
e (or E) means "times ten to the power of": 1e3 is 1000 and 2.5e-4 is 0.00025. Exponent notation is only for decimal literals. Inside a hex literal, e is just the hex digit 14, so 0x1e3 equals 483. JavaScript also uses exponent notation when it prints very large or very small values, such as 1e+21.
Numeric Separators
An underscore between digits makes long literals easier to read. The parser ignores the underscores.
const budget = 1_000_000;
const mask = 0b1111_0000;
const color = 0xff_99_00;
console.log(budget); // 1000000
console.log(mask); // 240
console.log(Number("1_000")); // NaN
console.log(parseInt("1_000", 10)); // 1An underscore is a SyntaxError when it is:
- at the start or end of the digits (
_100,100_) - doubled (
1__000) - next to the decimal point or the
e(1_.5,1e_3) - right after a prefix (
0x_ff) - right after a leading zero (
0_1)
Common Errors
- Legacy octal. In sloppy mode,
017is the octal value 15, while018is the decimal value 18. Strict mode and ES modules reject017with a SyntaxError. Write0o17when you mean octal. - Method call on an integer literal.
5.toString()is a SyntaxError because the parser reads5.as the number. Write(5).toString()or5..toString()instead. - Treating the minus sign as part of the literal. In
-0x10, the literal is0x10and the-is the unary minus operator applied to it. The result, -16, is still correct.
Exercise
What does console.log(0b1010 + 0o12 + 0xa + 1e1) print, and why?
Show answer
It prints 40. Each literal is the number 10 written in a different notation (binary, octal, hex, and exponent form), so the sum is 10 + 10 + 10 + 10.
Exercise
A config file contains the string "2_500". Why does Number() fail on it, and how can you convert it to 2500?
Show answer
Numeric separators are only valid in source code literals, so Number("2500") returns NaN. Remove the underscores first: Number("2500".replaceAll("_", "")) returns 2500.