Numeric literals with underscores in JavaScript [duplicate]
Asked Answered
S

2

17

I recently ran into this code and I was wondering if there are any differences in writing those numeric literals in JavaScript with or without underscores:

let number = 1234567890;
console.log(number);

let number2 = 123_4567_890;
console.log(number2);
Semantics answered 11/10, 2021 at 11:13 Comment(1)
J
15

The point of the Numeric Seperators is to visually aid developers when dealing with big numbers. They don't change the actual code or its meaning.

Jeromejeromy answered 11/10, 2021 at 11:16 Comment(0)
S
18

So after further reading, I see that ECMAScript 2021 has a new feature called Numeric separators which is used for larger numbers that are hard to read without separating them..

To improve readability, this feature enables underscores as separators in numeric literals as you can see in the following example for all kinds of numeric literals:

const readableMiliion = 1_000_000;
const regularMiliion = 1000000;

console.log(readableMiliion);
console.log(regularMiliion);     

const decimal = 1_000_000.220_720;
const binary = 0b01010110_00111000;
const hexa = 0x40_76_38_6A_73;

console.log(decimal);  
console.log(binary);  
console.log(hexa);  

More about Numeric Separators can be found on the following pages:

Semantics answered 11/10, 2021 at 15:31 Comment(0)
J
15

The point of the Numeric Seperators is to visually aid developers when dealing with big numbers. They don't change the actual code or its meaning.

Jeromejeromy answered 11/10, 2021 at 11:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.