Search code examples
javascriptnumbersformatreadabilitycode-readability

Making large numbers in Javascript code more readable


Is there a way to make a large number, i.e. 1000000, more readable in JS code?

I know in Ruby you can write 1_000_000 instead, which makes it a lot easier to identify visually.

Thanks.


Solution

  • I generally use multiplication in this situation; in this case I'd use 1000 * 1000. Other examples

    one megabytes is 1000 * 1024
    one hour (in milliseconds) is 60 * 60 * 1000
    

    etc. This isn't quite as nice as the underscore notation, but on the other hand, it requires no special language support.

    Another approach is to define "constants", for example:

    var ONE_MILLION = 1000000;
    

    or combining the two:

    var ONE_MILLION = 1000 * 1000;