Using methods with numbers in JavaScript

Yep! We have a bunch of weird and messed up shit in js... And how language allow us to apply methods on numbers is one of these lovely features!

Weired syntax (don't write like this)

 // number-methods.ts

console.log(69..toString()) // "69"
console.log(13.123123.toFixed())  // 13
console.log(666..toExponential()) // 6.66e+2

Usually we store number in variable and apply method on it with one dot. When we call methods on number literal we need some way to tell the interpreter, that we are calling method. So, the second dot signals that there is no decimal place and the method can be called.

// number-methods.ts

// ❗ syntax error
// 👇 will no work
console.log(13.123123..toFixed())

As you can guess something will go wrong, if try something like snippet above.

Encouraged syntax

If for some unbelievably reasonable reason you will be calling methods on literal values, use braces.

// number-methods.ts

console.log((69).toString()) // "69"
console.log((13.123123).toFixed()) // 13
console.log((666).toExponential()) // 6.66e+2

If your text editor or IDE formats code, it should insert braces automatically.

Why would I write entire post on something obscure and utterly useless like this? Well... Right now on the start I want to write about something fun😊