Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

JavaScript Numeric Separators (`_`)

1 min read .
JavaScript Numeric Separators (`_`)

JavaScript numeric separators let you place underscores inside numeric literals to improve readability. They do not change the numeric value.

const million = 1_000_000;
console.log(million); // 1000000

Decimal Fractions

const pi = 3.141_592_653;

Binary, Octal, and Hexadecimal

const binary = 0b1010_1011;
const octal = 0o123_456;
const hex = 0xFF_FF_FF;

Separators can also be used in BigInt literals:

const population = 8_000_000_000n;

Syntax Rules

Underscores must appear between digits. These are invalid:

// const a = _1000;
// const b = 1000_;
// const c = 1__000;
// const d = 3._14;

Numeric separators are source-code syntax; they are not accepted automatically when parsing strings:

Number('1_000'); // NaN

Conclusion

Numeric separators are a readability feature for source code. Use them to make long decimal values, bit masks, hexadecimal constants, and BigInts easier to scan without changing their runtime value.

Related Posts

chevron-up