Back to Blog
Image Description

Typescript Feature - The Numeric Separator

Typescript

Web Development

February 2, 2023

Long numbers, or numbers with a large amount of digits, can be difficult to read without commas or spaces. Typescript allows us to add separations by inserting an _ (underscore) symbol wherever we want in a number.

For example, let's look at:

1const bigNumber = 567845678;

At a glance, it's hard to determine which prefix to use without manually counting from the right to the left.

Now, using the numerical separator in Typescript, we can make the number easier to read:

1const bigNumber = 567_845_678;

This also applies to hex or binary digits:

1const bigNumberBinary = 0b100001_11011000_10100011_00101110; 2const bigNumberHex = 0x21D8_A32E

How is this possible?

The separator is removed during transpilation and the number is returned to its original form. his makes it compatible with older versions of ECMAScript since the new syntax is stripped out.


I found this interesting and I hope you did too! Thanks for reading!

  • CL