markmcdermott.io (js, ruby, ...etc) by mark mcdermott

Underscore Separators

05/01/2024

estimated reading time:1 min

Had a “whoa” moment today when I learned that both javascript and ruby accept underscores in numbers. Underscores are used where commas or periods would be and are just ignored when parsing the number.

JavaScript

> console.log(1_000)
1000
undefined
> console.log(1_000 + 2_000)
3000

Ruby

> puts 1_000_000
1000000
 => nil
> puts 1_000 + 2_000
3000