SyntaxError: Identifier directly after number in JavaScript

avatar

Borislav Hadzhiev

Last updated: Mar 8, 2022

banner

Photo from Unsplash

SyntaxError: Identifier directly after number in JavaScript #

The JavaScript error "Identifier directly after number" occurs when a variable starts with a digit or we try to call a method directly on a digit. To solve the error, rename your variables to not start with a digit and wrap numeric literal in parenthesis when calling methods.

Here are 2 examples of how the error occurs.

index.js
// 👇️ Because we call method directly on digit // ⛔️ SyntaxError: Identifier directly after number const result1 = 5.toFixed(2); const result2 = 5.toString() // 👇️ Because variable (or identifier) starts with digit // ⛔️ SyntaxError: Identifier directly after number const 501employee = 'James Doe'

In the first example, we call a method directly on a numeric literal and get the error.

To get around this, we have to wrap the numeric literal in parentheses.

index.js
const result1 = (5).toFixed(2); console.log(result1); // 👉️ "5.00" const result2 = (5).toString(); console.log(result2); // 👉️ "5"

JavaScript won't allow us to call a method directly on a numeric literal, so we had to wrap it in parentheses to solve the error.

When you think about it, it makes sense because if the number has a decimal, it is kind of confusing which dot is used to separate the integer from the decimal and which is the method invocation.

An alternative solution is to extract the number into a variable.

index.js
const num = 5; const result1 = num.toFixed(2); console.log(result1); // 👉️ "5.00"

You might also get the error if you have declared a variable whose name starts with a digit.

index.js
// ⛔️ SyntaxError: Identifier directly after number const 501employee = 'James Doe'
In JavaScript, all variable names must start with a letter, an underscore _ or a dollar sign $. This means you aren't allowed to start a variable name with a digit.

However, you are able to use digits for the subsequent characters of a variable name.

index.js
const employee501 = 'James Doe'; console.log(employee501); // 👉️ "James Doe"

This rule also applies to object properties and any other identifiers.

index.js
const company = { // ⛔️ Identifier starts with number 501employee: 'James Doe', }

Make sure to start the names of your identifiers with a letter, an underscore _ or a dollar sign $ to avoid this error.

index.js
const company = { employee501: 'James Doe', }; console.log(company.employee501); // 👉️ "James Doe"

Conclusion #

The JavaScript error "Identifier directly after number" occurs when a variable starts with a digit or we try to call a method directly on a digit. To solve the error, rename your variables to not start with a digit and wrap numeric literal in parenthesis when calling methods.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.