Numeric Separator In JavaScript || Underscore Between Digit In JavaScript

Hello Devs,

In this article let's discuss about one of the most amazing features introduced in ECMAScript21 (12th edition) i.e., the Numeric Separator.

Table of content

  1. Introduction
  2. Some Examples
  3. Conclusion

1. Introduction

This feature enables developers to make the numeric literals more readable. What I mean to say is, large numeric literals (both integers and floating-point) which are difficult for the human eye to parse quickly, especially when there are long digit repetitions, this feature of ES12 makes it simple to read.


Please read this number

let num = 1000000000000

if someone is reading the value of num verbally, then at the first sight one cannot be able to read it. So numeric separator help developers make it readable at the first sight.

Let's see how it helps.

Now, read this number

let num = 100_000_000_000

Did you find it a little easier to read than the first one? If YES let me know

2. Examples

Let's see some examples,

  1. equality & strict equality
let num1 = 1000000000000 let num2 = 100_000_000_000 console.log(num1==num2) //true console.log(num1===num2) //true
  1. What is the output in the console for a number with an underscore in it?
let num = 100_000_000_000 console.log(num) //100000000
  1. We can use underscore with Binary, Octal, Hex numbers are well
let num1 = 0b1010_0101_1001 // binary let num2 = 0o2_3_5_7 // octal let num3 = 0xA_B_C_D_E // hex
  1. underscore with String Literals.
let str1="hellocapscode" let str2="hello_capscode" console.log(str1==str2)//false as numeric separator works well only with numbers console.log(str1) //hellocapscode console.log(str2) //hello_capscode

3. Conclusion

Awesome! Now you have learned 1 new feature of JavaScript, I hope you have now a very clear idea regarding the underscore between digits in numeric literals.

Thank you for reading this far. This is a brief introduction to Numeric Separator In JavaScript.

If you find this article useful, share this article. Someone could find it useful too. If you find anything technically inaccurate, please feel free to create an issue.

Hope it's a nice and informative read for you. VISIT https://www.capscode.in/blog TO LEARN MORE... See you in my next Blog article, Take care!!