How to chain a method to a number literal
SyntaxError: identifier starts immediately after numeric literal
Table of contents
In JavaScript, you can chain a method to a string literal.
Example using
.toLowerCase
method:
const sentence = "A cool sentence".toLowerCase();
console.log(sentence);
But this will not work with a number literal, example using .toString
:
console.log(234.toString());
Depending on the runtime you can get different error messages, for Example in Deno you get:
parse error: Identifier cannot follow number
How to "fix it"?
You can easily overcome this
console.log((234).toString());
This looks a little weird but will work too
console.log(278..toString());
This one works too!
console.log(458 .toString());
Note the space
Real word example
You want to get the exponential of a number using toExponential
๐
console.log(1_000_000 .toExponential()); //=> '1e+6'
ย