JavaScript Numeric Separator (`_`)

2 min read .

The Numeric Separator (_) in JavaScript is a feature that enhances code readability by allowing underscores to be placed within numeric literals. This is particularly useful when working with large numbers, making them easier to read and understand at a glance.

What is the Numeric Separator (_) in JavaScript?

The Numeric Separator (_) is an underscore character (_) that can be inserted between digits of a number literal to visually separate groups of digits. It does not affect the value of the number; it is purely for human readability.

Syntax:

const readableNumber = 1_000_000; // 1 million

The underscores help visually separate the number into groups, making it easier to read and interpret.

How Does the Numeric Separator Work?

  • Purpose: The Numeric Separator (_) is designed to make numeric literals in JavaScript more readable by visually grouping digits.
  • Use Cases: It is particularly useful in financial calculations, scientific computations, or any other domain where large or complex numbers are common.

Examples of Using the Numeric Separator

  1. Improving Readability of Large Numbers
const billion = 1_000_000_000; // 1 billion
console.log(billion); // Output: 1000000000
  1. Using with Decimal Numbers
const decimalNumber = 3.141_592_653; // Pi approximation
console.log(decimalNumber); // Output: 3.141592653
  1. Using with Binary, Octal, and Hexadecimal Numbers

You can also use the numeric separator with other numeric bases:

const binaryNumber = 0b1010_1011; // Binary representation
console.log(binaryNumber); // Output: 171

const hexNumber = 0xFF_FF_FF; // Hexadecimal representation
console.log(hexNumber); // Output: 16777215

const octalNumber = 0o123_456; // Octal representation
console.log(octalNumber); // Output: 42798

Rules for Using the Numeric Separator

  • The underscore (_) can be placed between any two digits in a numeric literal.
  • It cannot be placed at the beginning or end of a number, adjacent to a decimal point, or next to another underscore.

Examples of Invalid Usages:

const invalidNumber1 = _1000;  // SyntaxError
const invalidNumber2 = 1000_;  // SyntaxError
const invalidNumber3 = 1__000; // SyntaxError
const invalidNumber4 = 3._14;  // SyntaxError

Benefits of Using the Numeric Separator

  • Enhanced Readability: Makes numbers easier to read, reducing errors when dealing with large or complex values.
  • Improved Maintenance: Easier to maintain and review code, especially when handling numbers with multiple digits.
  • Universal Compatibility: Works with all numeric types, including integers, floats, binary, octal, and hexadecimal.

Browser Compatibility

The Numeric Separator (_) is supported in most modern browsers, including Chrome (version 75+), Firefox (version 70+), Safari (version 13+), and Edge (version 79+). It is not supported in Internet Explorer, so if you need compatibility with older browsers, you might need to avoid using this feature.

Conclusion

The JavaScript Numeric Separator (_) is a simple yet effective tool for improving the readability of numeric literals. By using this feature, developers can write cleaner, more understandable code, particularly when working with large or complex numbers.

See Also

chevron-up