ES2021 - Numeric Operators

This tutorial covers latest javascript features,ES12 features numeric separator method - Applied Underscore separator to numeric,float,hexa,octa,binary and bigint literal types, fraction and exponent parts. ..

This post covers an feature numeric operators introduced in latest JavaScript ECMAscript 2021.

This is only cosmetic changes how we read the literals and numbers.

Numeric operators applies to numbers or literals allows developers to read them by separating group of digits. _ or underscore symbol is used as a separator.

For example,

if you are defining number of 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 for floating literals

const floatNumber=10.23;  // 10^10000 

And also applied to fraction and exponent parts

const exponentNumber=1e2_000;  // 10^10000 

Here is 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 are separated with numeric operators

let TwoTrillion = 2_000_000_000_000n;

The following are invalid separators which throws an error

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

Notes:

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