99

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?

psychotik
  • 38,153
  • 34
  • 100
  • 135

20 Answers20

155

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.

amercader
  • 4,490
  • 2
  • 24
  • 26
  • it was a missing ; in a event assignment. The clue to look at dynamically created objects helped pick this up. jslint didn't find this initially coz it was generated as inline js as part of a html page. Thx! – psychotik Jan 27 '10 at 20:48
  • @psychotik: Debugging would have shown you this without making you search for it... – ErikE Jan 28 '10 at 19:06
  • 2
    No it wouldn't. Most modern browsers fix this for you. IE6/7 doesn't. And debugging IE8 in IE7 emulation mode doesn't catch this. (I'm guessing you know that IE7 doesn't have a debugger - using VS with IE7 doesn't catch this either). Try it and then tell me ;) – psychotik Jan 28 '10 at 20:28
  • @psychotik: you are absolutely right. My apologies. I am updating my answer. – ErikE Jan 29 '10 at 00:13
  • 1
    Thank you! Apparently this is not a problem in other browsers, like Chrome. – B Seven Aug 11 '11 at 17:34
  • 5
    You can search such occurrences using regex ",\s*\]" and ",\s*\}" (without quotes). Saved me a lot of time. – Gokhan Kurt Apr 01 '16 at 14:42
  • 2
    Regex for javascript with line breaks like this: `,\s*?\n?\s*?]` and `,\s*?\n?\s*?\}` – hayatbiralem May 21 '16 at 12:42
85

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.

Roy Hyunjin Han
  • 4,725
  • 2
  • 30
  • 23
  • Nice one, I'm struggling with this problem for hours. Solved now. – John Prado Jan 25 '12 at 21:52
  • Thank you good sir for saving me from staring at this javascript library for any longer than necessary. – Khepri Apr 18 '12 at 06:19
  • 1
    I submitted a bug to JSHint, hopefully they'll be able to detect these sorts of errors in the future: https://github.com/jshint/jshint/issues/1000 – travis Apr 10 '13 at 19:11
  • 1
    @travis They actually allowed it as a result of [#674](https://github.com/jshint/jshint/issues/674/) :-) – cmbuckley May 07 '13 at 13:30
  • 3
    I had exactly same error, but on property "import". Here is a full list of "Reserved Words" https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Reserved_Words – Edgar Zagórski Jan 08 '14 at 15:35
  • My error came from using the delete keyword in angular.js: $http.delete() needs to be $http['delete']() – Henry Feb 23 '15 at 23:18
11

Actually I got something like that on IE recently and it was related to JavaScript syntax "errors". I say error in quotes because it was fine everywhere but on IE. This was under IE6. The problem was related to JSON object creation and an extra comma, such as

{ one:1, two:2, three:3, }

IE6 really doesn't like that comma after 3. You might look for something like that, touchy little syntax formality issues.

Yeah, I thought the multi-million line number in my 25 line JavaScript was interesting too.

Good luck.

cjstehno
  • 13,468
  • 4
  • 44
  • 56
9

This is a definitive un-answer: eliminating a tempting-but-wrong answer to help others navigate toward correct answers.

It might seem like debugging would highlight the problem. However, the only browser the problem occurs in is IE, and in IE you can only debug code that was part of the original document. For dynamically added code, the debugger just shows the body element as the current instruction, and IE claims the error happened on a huge line number.

Here's a sample web page that will demonstrate this problem in IE:

<html>
<head>
<title>javascript debug test</title>
</head>
<body onload="attachScript();">
<script type="text/javascript">
function attachScript() {
   var s = document.createElement("script");
   s.setAttribute("type", "text/javascript");
   document.body.appendChild(s);
   s.text = "var a = document.getElementById('nonexistent'); alert(a.tagName);"
}
</script>
</body>

This yielded for me the following error:

Line: 54654408
Error: Object required
ErikE
  • 48,881
  • 23
  • 151
  • 196
  • @Protectorone Temporarily put the javascript in the document itself via a ` – ErikE Jan 31 '12 at 19:26
5

Just saw the bug in one of my applications, as a catch-all, remember to enclose the name of all javascript properties that are the same as keyword.

Found this bug after attending to a bug where an object such as:

var x = { class: 'myClass', function: 'myFunction'};

generated the error (class and function are keywords) this was fixed by adding quotes

var x = { 'class': 'myClass', 'function': 'myFunction'};

I hope to save you some time

Josue Alexander Ibarra
  • 8,269
  • 3
  • 30
  • 37
  • I ran into the same issue. My code was using something like `if(_features.delete){...}` which was causing an error because delete is a keyword. http://stackoverflow.com/questions/26255/reserved-keywords-in-javascript – Snekse Jan 07 '14 at 23:08
4

http://closure-compiler.appspot.com/home will pick this error up with an accurate reference to the actual line number in the offending script.

chopstik
  • 363
  • 2
  • 10
2

As noted previously, having an extra comma threw an error.

Also in IE 7.0, not having a semicolon at the end of a line caused an error. It works fine in Safari and Chrome (with no errors in console).

B Seven
  • 44,484
  • 66
  • 240
  • 385
2

IE7 is much less forgiving than newer browsers, especially Chrome. I like to use JSLint to find these bugs. It will find these improperly placed commas, among other things. You will probably want to activate the option to ignore improper whitespace.

In addition to improperly placed commas, at this blog in the comments someone reported:

I've been hunting down an error that only said "Expected identifier" only in IE (7). My research led me to this page. After some frustration, it turned out that the problem that I used a reserved word as a function name ("switch"). THe error wasn't clear and it pointed to the wrong line number.

Muhd
  • 24,305
  • 22
  • 61
  • 78
1

This error occurs when we add or missed to remove a comma at the end of array or in function code. It is necessary to observe the entire code of a web page for such error.

I got it in a Facebook app code while I was coding for a Facebook API.

<div id='fb-root'>
    <script type='text/javascript' src='http://connect.facebook.net/en_US/all.js'</script>
    <script type='text/javascript'>
          window.fbAsyncInit = function() {
             FB.init({appId:'".$appid."', status: true, cookie: true, xfbml: true});            
             FB.Canvas.setSize({ width: 800 , height: 860 , }); 
                                                       // ^ extra comma here
          };
    </script>
indiv
  • 17,306
  • 6
  • 61
  • 82
Tousif Jamadar
  • 141
  • 1
  • 7
1

Remove the unwanted , sign in the function. you will get the solution.

Refer this

http://blog.favrik.com/2007/11/29/ie7-error-expected-identifier-string-or-number/

king4test
  • 21
  • 1
  • You're solution is correct, I upvoted you to eliminate the -1 downvote. Not sure why someone would downvote a correct answer. – akousmata Mar 15 '13 at 15:01
0

IE7 has problems with arrays of objects

columns: [
{
  field: "id",
  header: "ID"
},
{
  field: "name",
  header: "Name" , /* this comma was the problem*/ 
},
...
Stefan Michev
  • 4,795
  • 3
  • 35
  • 30
0

Another variation of this bug: I had a function named 'continue' and since it's a reserved word it threw this error. I had to rename my function 'continueClick'

Kevin Audleman
  • 334
  • 2
  • 4
0

This sounds to me like a script that was pulled in with src, and loaded just halfway, causing a syntax error sine the remainder is not loaded.

Roland Bouman
  • 31,125
  • 6
  • 66
  • 67
0

Maybe you've got an object having a method 'constructor' and try to invoke that one.

Niels Steenbeek
  • 4,692
  • 2
  • 41
  • 50
0

You may hit this problem while using Knockout JS. If you try setting class attribute like the example below it will fail:

<span data-bind="attr: { class: something() }"></span>

Escape the class string like this:

<span data-bind="attr: { 'class': something() }"></span>

My 2 cents.

iDevGeek
  • 464
  • 5
  • 4
0

I too had come across this issue. I found below two solutions. 1). Same as mentioned by others above, remove extra comma from JSON object. 2). Also, My JSP/HTML was having . Because of this it was triggering browser's old mode which was giving JS error for extra comma. When used it triggers browser's HTML5 mode(If supported) and it works fine even with Extra Comma just like any other browsers FF, Chrome etc.

0

Here is a easy technique to debug the problem: echo out the script/code to the console. Copy the code from the console into your IDE. Most IDE's perform error checking on the code and highlight errors. You should be able to see the error almost immediately in your JavaScript/HTML editor.

hisenberg
  • 140
  • 1
  • 5
0

Had the same issue with a different configuration. This was in an angular factory definition, but I assume it could happen elsewhere as well:

angular.module("myModule").factory("myFactory", function(){
    return
    {
        myMethod : function() // <--- error showing up here
        {
            // method definition
        } 
    }
});

Fix is very exotic:

angular.module("myModule").factory("myFactory", function(){
    return { // <--- notice the absence of the return line
        myMethod : function()
        {
            // method definition
        } 
    }
});
wiwi
  • 270
  • 2
  • 9
0

This can also happen in Typescript if you call a function in middle of nowhere inside a class. For example

class Dojo implements Sensei {
     console.log('Hi'); // ERROR Identifier expected.
     constructor(){}
}

Function calls, like console.log() must be inside functions. Not in the area where you should be declaring class fields.

rayray
  • 1,625
  • 1
  • 9
  • 15
0

Typescript for Windows issue

This works in IE, chrome, FF

export const OTP_CLOSE = { 'outcomeCode': 'OTP_CLOSE' };

This works in chrome, FF, Does not work in IE 11

export const OTP_CLOSE = { outcomeCode: 'OTP_CLOSE' };

I guess it somehow related to Windows reserved wordsenter image description here

Lev Savranskiy
  • 422
  • 2
  • 7
  • 19