This post covers a feature of numeric operators introduced in the latest JavaScript ECMAScript 2021.

These are only cosmetic changes in how we read the literal and numbers.

Numeric operators apply to numbers or literal allowing developers to read them by separating a group of digits. _ or underscore symbol used as a separator.

For example,

if you are defining the number as One million as seen below

let result=1000000;

It is hard to read the number whether is one million or one lack thousand number.

The same number can be separated with _ as per your convenience in ES12.

let result=10_00_000;

Valid Numeric separators

Numeric separators are applied to number,float, binary, hex, octal and bigint literal types

Here is an example of floating literal

const floatNumber=10.23;  // 10^10000 

And also applied to fraction and exponent parts.

const exponentNumber=1e2_000;  // 10^10000 

Here are Hexa, octal, and binary literals example

const binaryNumber= 0b1110_0001_1010_0101; // binary number separation
const hexaNumber = 0xB1_C8_D1;
const octalNumber = 0o45_23_12;

big numbers in JavaScript represented in BigInt type

bigint numbers separated with numeric operators.

let TwoTrillion = 2_000_000_000_000n;

The following are invalid separators that throws an error

const floating1 = 4.54_35_;          // SyntaxError
const invalid_separator = _1_000_000;    // ReferenceError

Notes:

  • _ applies to any digits between the numbers only.
  • Starting and Ending digits can not be applied underscore.
  • numeric digits are not separated not immediately before n in bigint literal types.