UI Development

JAVASCRIPT NUMERIC SEPARATORS

Since times immemorial developers have had a love-hate relationship with numbers and data types in general  in JavaScript. As much as the amount of resources we have today to know and learn more about the soul of web development i.e JavaScript, our relation with Numbers is still isn’t too smooth but with newer advancements it is kind of getting better with each day. One such thing is the emergence of Numeric Separators in the JavaScript world.

Introduction

As the title suggests, the entities which separate large numerals in JavaScript are termed as JavaScript Numeric Separators. A lot of times developers are in a position where they have to deal with massively large numericals, it is in those conditions these separators come in handy.

For example, on reading const 1000000000, the developer might think Is this a billion? a hundred millions? Ten millions?

It becomes very difficult for the human eye to read and parse large numeric literals quickly, specifically when there are lots of repeating digits:

For improving the readability, we can use underscore characters as separators in numeric literals, it is a new JavaScript language feature. So, the above numbers can now be rewritten to group the digits per thousand, for example:

Now it’s relatively easier to tell that the first number is a trillion, and the second number is in the order of 9 billion.

_ as a separator appears in other programming languages also like Java, Ruby, Python, Perl,  Rust, Julia, Ada, C#

Usability

An underscore character _ can be included anywhere and used between the number to improve readability. Multiple underscore characters can be used in a numeric literals.

We can use these separators at the appropriate places whenever needed, for instance, digits can be grouped per thousand.

Note: Numeric separators are just for code readability, and will not change the value of the number.

Advantages

JavaScript numeric separators have been introduced to promote readability for the developers so that it leads to lesser bugs when dealing with very large numerical literals.

Limitations

Usage of numeric separators have the following limitations :

  • Multiple underscores cannot be included one after the other.

  • Underscore cannot be included at the end of the number.

  • Underscores cannot be included after a leading 0.

  • Underscores can be included only between two digits, the following is invalid.

Conclusion

  • JavaScript numeric separators are used to improve the readability but they should be used wherever required and not everywhere.
    For example,

JavaScript also has an inbuilt legacy syntax for octal literals without the 0o prefix explicitly. For example, 017 === 0o17. This syntax does not have support in strict mode or within modules, and it is not advisable to be used in modern code. And, numeric separators do not have support for these literals. Use 0o17-style literals instead.

  • Don’t forget about exponential notation
    With more number of trailing zeros, exponential notation might serve to be more convenient than grouping zeros. Compare:

  •  Numbers aren’t always the best choice to represent numeric or semi-numeric data.
    Some data such as credit card numbers, phone numbers and social security numbers etc, are in some ways numbers, in other ways not: There may be non-numeric prefixes and separators and leading digits are significant. They should also not be represented in exponential notation.
    Therefore  avoid:

If used efficiently, these Separators can be helpful for the developer community in specific and might prevent numbers related issues going further.

About The Author