Eliaz Bobadilla
UltiRequiem's Blog

UltiRequiem's Blog

How to chain a method to a number literal

How to chain a method to a number literal

SyntaxError: identifier starts immediately after numeric literal

Eliaz Bobadilla's photo
Eliaz Bobadilla
·Feb 10, 2022·

1 min read

Table of contents

  • How to "fix it"?
  • Real word example

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'
 
Share this