Skip to content

Numeric Separators

A numeric separator is an underscore placed between the digits of a number literal, such as 1_000_000. JavaScript ignores it, so the value is exactly the same as without it, but a reader can count the digits at a glance. Separators arrived in ES2021 and work in every current browser and in Node.js.

Underscores can go in whole numbers, decimals, and binary, octal and hexadecimal literals, wherever a grouping helps the reader:

const visitors = 67_596_281;
const fee = 1_250.50;
const mask = 0b1111_0000;
const colour = 0xff_aa_33;
console.log(visitors);
console.log(fee);
console.log(mask);
console.log(colour);

This prints:

67596281
1250.5
240
16755251

The separators do not appear in the output, because they exist only in the source code. The grouping need not be in threes: 0b1111_0000 groups by four bits, and 0xff_aa_33 groups the red, green and blue bytes of a colour.

Separators are most useful for very long numbers, which are often BigInt values. A Number can only hold whole numbers exactly up to Number.MAX_SAFE_INTEGER, while a BigInt, written with an n suffix, holds whole numbers of any size:

const largest = Number.MAX_SAFE_INTEGER;
console.log(largest);
console.log(largest + 2);
const stars = 400_000_000_000_000_000_000n;
console.log(stars);
console.log(stars + 2n);

This prints:

9007199254740991
9007199254740992
400000000000000000000n
400000000000000000002n

largest + 2 should be 9007199254740993, but that value cannot be stored as a Number, so the result is rounded to the nearest one that can. The BigInt sum is exact.

An underscore must sit between two digits. Each of these is a syntax error:

Literal Why it fails
1__000 two underscores in a row
1000_ an underscore at the end
1._5 an underscore next to the decimal point
0_1 an underscore after a leading zero

A leading underscore, as in _1000, is not an error but is not a number either, because JavaScript reads it as the name of a variable.

Separators are part of the syntax for number literals. Functions that convert text to a number do not accept them:

console.log(Number('1_000'));
console.log(parseInt('1_000', 10));

This prints:

NaN
1

Number rejects the whole string, and parseInt stops reading at the underscore. Text typed by a user, or read from a file, needs its separators removed before conversion, for example with text.replaceAll('_', ''). ParseInt and ParseFloat covers converting text in more detail.

Steve Griffith covers this topic in his video New JavaScript Numeric Separators.