Sub topic 2
Writing Numbers: Decimal, Hex, Octal, Binary, Exponents, and Separators
This chapter assumes you can run JavaScript in Node.js or a browser console, and that you know === and typeof. It also builds on the previous chapter: every number you write ends up as one 64-bit IEEE 754 double, whatever syntax you used to write it.
A numeric literal is a number written directly in source code, such as 42 or 0xff. JavaScript gives you several literal forms. They exist only for readability. After parsing, 255, 0xff, 0o377, and 0b11111111 are the same value, and the engine keeps no record of which form you used.
The Literal Forms at a Glance
| Form | Prefix | Allowed digits | Example | Value |
|---|---|---|---|---|
| Decimal | none | 0–9, `.`, `e` | `255` | 255 |
| Exponent | none | decimal plus `e`/`E` | `2.55e2` | 255 |
| Hexadecimal | `0x` or `0X` | 0–9, a–f, A–F | `0xff` | 255 |
| Octal | `0o` or `0O` | 0–7 | `0o377` | 255 |
| Binary | `0b` or `0B` | 0, 1 | `0b11111111` | 255 |
| Legacy octal | leading `0` | 0–7 | `0377` | 255 (sloppy mode only) |
console.log(255, 0xff, 0o377, 0b11111111); // 255 255 255 255
console.log(0xFF === 255); // true
console.log(typeof 0b101); // "number"console.log always prints in decimal. To show a value in another base, you convert it to a string. A later chapter covers that with toString(radix).
Decimal and Exponent Notation
Decimal literals can have a fractional part. Either side of the decimal point can be left out:
console.log(.5); // 0.5
console.log(5.); // 5
console.log(5.0); // 5Exponent notation writes a number as a mantissa times a power of ten. e (or E) means "times ten to the power of", and the exponent can carry a sign:
console.log(1e3); // 1000
console.log(1E6); // 1000000
console.log(2.5e-4); // 0.00025
console.log(1e21); // 1e+21 (large values print in exponent form)Exponent notation suits very large and very small values, such as physical constants or timeouts in nanoseconds. A literal outside the double range gets no error. The engine rounds it silently:
console.log(1e309); // Infinity (above the largest double)
console.log(5e-324); // 5e-324 (the smallest positive double)
console.log(2e-324); // 0 (rounds down to zero)The same silent rounding applies to integers. 9007199254740993 is a legal literal, but it is stored as 9007199254740992. The chapter on the safe integer range explains why.
Hexadecimal, Octal, and Binary
The three prefixed forms only write integers. They take no decimal point and no exponent. In hex, e is a digit, so 0x1e3 is 483 and not 1000.
- Hexadecimal (
0x) is the usual way to write colors, byte values, bit masks, and Unicode code points:0xff0000,0x1F600. - Octal (
0o) shows up mostly in Unix file permissions:0o755. - Binary (
0b) is the clearest way to write bit flags:0b0100.
const READ = 0b100;
const WRITE = 0b010;
const EXEC = 0b001;
const mode = 0o755;
console.log(READ | WRITE); // 6
console.log(mode); // 493
console.log(0x1e3); // 483
console.log(0xff_ff_ff); // 16777215The Legacy Octal Trap
Before ES2015, a leading zero meant octal in sloppy (non-strict) mode, so 010 was 8. A leading zero followed by an 8 or 9 (08, 019) quietly fell back to decimal. Strict mode rejects both forms with a SyntaxError, and ES modules and class bodies always run in strict mode.
console.log(Function("return 010")()); // 8 (sloppy mode: legacy octal)
console.log(Function("return 019")()); // 19 (sloppy mode: decimal fallback)
try {
Function('"use strict"; return 010');
} catch (e) {
console.log(e.name); // "SyntaxError"
}Numeric Separators
ES2021 added the underscore _ as a numeric separator. It groups digits so you can read them, and the engine ignores it:
console.log(1_000_000 === 1000000); // true
console.log(0b1010_0001); // 161
console.log(0xFF_FF); // 65535
console.log(1_000.000_1); // 1000.0001
console.log(1e1_0); // 10000000000The separator can only appear between two digits. These placements are all SyntaxErrors:
- at the end:
1000_ - doubled:
1__000 - next to the decimal point:
1_.5,1._5 - next to
e:1e_3,1_e3 - straight after a prefix:
0x_ff - after a leading zero:
0_1
A leading underscore doesn't cause a syntax error, which can be confusing. _1000 is a valid identifier name. It parses fine and then throws a ReferenceError at run time, unless a variable with that name happens to exist.
The following script checks these rules by asking the parser directly. Function parses its body when the function is created, so invalid syntax throws right away:
function parses(src) {
try {
Function('"use strict"; return (' + src + ");");
return true;
} catch (e) {
return false;
}
}
const samples = [
"1_000_000", "1000_", "1__000", "1_.5", "1._5",
"1e_3", "0x_ff", "0_1", "0b1010_0001", "1e1_0", "010", "08"
];
for (const s of samples) {
console.log(s.padEnd(12), parses(s));
}
// 1_000_000 true
// 1000_ false
// 1__000 false
// 1_.5 false
// 1._5 false
// 1e_3 false
// 0x_ff false
// 0_1 false
// 0b1010_0001 true
// 1e1_0 true
// 010 false
// 08 falseSeparators Do Not Work in Strings
Separators are part of source-code syntax only. String-to-number conversion doesn't recognize them:
console.log(Number("1_000")); // NaN
console.log(parseInt("1_000", 10)); // 1 (stops at the underscore)
console.log(parseFloat("1_000.5")); // 1So if user input or a config file contains 1_000, you have to remove the underscores yourself before converting. The same rule applies to formatted output: Intl.NumberFormat prints 1,000, never 1_000.
Literal Syntax Inside Strings
Number() and unary plus recognize some literal forms in strings, but not all of them. The next chapters cover conversion in detail. This table shows where string parsing differs from source syntax:
| Expression | Result | Why |
|---|---|---|
| `Number("0x1F")` | 31 | prefixes are recognized |
| `Number("0b101")` | 5 | prefixes are recognized |
| `Number("017")` | 17 | a leading zero means decimal, never octal |
| `Number("-0x1F")` | NaN | no sign allowed with a prefix |
| `Number("1e3")` | 1000 | exponent is recognized |
| `parseInt("0x1F")` | 31 | parseInt accepts the hex prefix only |
| `parseInt("0b101")` | 0 | reads "0", stops at "b" |
| `parseFloat("1e3")` | 1000 | parseFloat reads exponents |
Calling Methods on Literals
An integer literal followed directly by a dot is a common surprise. The parser reads the dot as a decimal point, so 5.toString() is a SyntaxError. Any of these forms work:
console.log((5).toString()); // "5"
console.log(5..toString()); // "5" (first dot is the decimal point)
console.log(5 .toString()); // "5" (space ends the literal)
console.log(5.0.toString()); // "5"
console.log(0xff.toString(2)); // "11111111" (hex has no decimal point)Use parentheses. They read most clearly, and formatters keep them.
Common Errors
- Writing
0x1.8or0b1.1. Prefixed literals can't have fractions. - Expecting
0x1e3to mean 1000. - Pasting
1_000into a string and converting it, which givesNaN. - Padding with leading zeros, which gives legacy octal or a SyntaxError.
- Writing a huge integer literal and assuming it is stored exactly.
Exercise
What does each line print, and which line fails to parse in strict mode? (a) console.log(0o17 + 0b11) (b) console.log(0x1e2) (c) console.log(017) (d) console.log(Number("0o17"))
Show answer
(a) 18, because 0o17 is 15 and 0b11 is 3. (b) 482, because e is a hex digit: 1×256 + 14×16 + 2. (c) In strict mode this is a SyntaxError (legacy octal). In sloppy mode it prints 15. (d) 15, because Number() recognizes the 0o prefix in strings.
Exercise
Which of these are valid literals: 1_000_000, 0x_FF, 3.141_592, 1_e5, 0b1111_0000, 100_? For each invalid one, name the rule it breaks.
Show answer
Valid: 1000000, 3.141592, 0b11110000. Invalid: 0xFF (separator straight after the prefix), 1e5 (separator next to the exponent marker), 100_ (trailing separator). A separator must sit between two digits.
Exercise
A config file contains the string "250000". Write one expression that converts it to the number 250000, and explain why Number("250000") doesn't work.
Show answer
Number("250000".replaceAll("", "")) returns 250000. Numeric separators belong to source-code syntax only. String-to-number conversion doesn't recognize them, so Number("250_000") returns NaN.