Skip to content

Numeric separators in JavaScript

In this post we will learn about the new feature in JavaScript called Numeric Separators which will be introduced in ES2021. Numeric separators enables developers to make their numeric literals more readable by creating a visual separation between groups of digits.

It is difficult for human eyes to parse long numeric literals quickly.

let total_limit = 1000000000000;

//What's this? A billion? 100 million? 1 trillion? Confusing.

This can be written with numeric separators like..

let total_limit = 1_000_000_000_000;

//So this is 1 trillion. Easily readable with the numeric separators.

Let’s see more examples.

const price = 74_99; // 74 dollars and 99 cents.

const color = 0xA3_3A_3A; // Color code in hex treated as three bytes.

let fraction = 0.00_1; //One thousanth.

let nibbles = 0b1010_0101_1100_1101; //binary nibbles

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.