Why is the underscore ignored in the JavaScript declaration of the number variable?

Kiran kumar
Apr 13, 2023

--

In JavaScript, underscores are not actually removed from variable declarations. Instead, they are simply ignored by the JavaScript parser when parsing numeric literals.

The reason underscores are often used in numeric literals is to make large numbers more readable for humans. For example, the number 1000000 can be hard to read and count the number of zeros, but using underscores can make it easier to understand at a glance

This is purely a visual convenience for human readers, and does not affect the actual value of the numeric literal. When the JavaScript code is executed, the underscore characters are simply ignored and the number is treated as if it were written without them:

Note that while underscores can be used in numeric literals in JavaScript, they cannot be used in variable or function names.

--

--

Kiran kumar
Kiran kumar

No responses yet