Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible cases for Javascript error: "Expected identifier, string or number"

Tags:

javascript

Some users are reporting occasional JS errors on my site. The error message says "Expected identifier, string or number" and the line number is 423725915, which is just an arbitrary number and changes for each report when this occurs. This mostly happens with IE7/ Mozilla 4.0 browsers.

I scanned my code a bunch of times and ran jslint but it didn't pick anything up - anyone know of the general type of JS problems that lead to this error message?

like image 870
psychotik Avatar asked Jan 27 '10 19:01

psychotik


People also ask

What does identifier expected mean Javascript?

The identifier expected error is a compilation error, which means the code doesn't comply with the syntax rules of the Java language. For instance, one of the rules is that there should be a semicolon at the end of every statement. Missing the semicolon will cause a compilation error.

What does identifier expected mean in Visual Basic?

When you see the Expected: Identifier Error message, it means that you use the reserved words in Visual Basic and it is forbidden to use the reserved words as the name of consts or variables.


2 Answers

The cause of this type of error can often be a misplaced comma in an object or array definition:

var obj = {    id: 23,    name: "test",  <-- } 

If it appears at a random line, maybe it's part of an object defintion you are creating dynamically.

like image 79
amercader Avatar answered Sep 25 '22 12:09

amercader


Using the word class as a key in a Javascript dictionary can also trigger the dreaded "Expected identifier, string or number" error because class is a reserved keyword in Internet Explorer.

BAD

{ class : 'overlay'} // ERROR: Expected identifier, string or number 

GOOD

{'class': 'overlay'} 

When using a reserved keyword as a key in a Javascript dictionary, enclose the key in quotes.

Hope this hint saves you a day of debugging hell.

like image 26
Roy Hyunjin Han Avatar answered Sep 23 '22 12:09

Roy Hyunjin Han