JavaScript Error ‘expected identifier, string or number’ in IE
Sometimes you get those errors that just pick at you and you can’t seem to find a solution. It happened to me this morning when testing a JavaScript library I’m writing in IE7. FireFox worked without a hitch, but IE threw an ‘expected identifier, string or number’ error pointing to the last line of the variable declaration. Of course it didn’t help that my copy of MS Script Debugger wanted to lock up my computer.
Here’s a very shortened example of the code I was working on.
-
var variableName =
-
{
-
function1: function()
-
{
-
// Do something
-
},
-
function2: function()
-
{
-
// Do something else
-
},
-
variable1: ‘Some value’,
-
variable2: ‘Another value’,
-
};
The error was reporting a problem on line 13. So after hacking my way around trying to figure out why it was giving me this error, I went out Googling. About 10 pages into the search results I found the solution.
Look at line 12. See the comma after ‘Another value’? That’s what was causing the error. I had taken out a variable under that one but forgot to remove the comma. FireFox ignored the error, but not IE.
One little misplaced character, so much wasted time. Hopefully y’all will spend less time finding the fix than I did.
Question, Comments...
Do you have more questions. Please either leave a comment below or join us in our new forum.
Thanks for the heads-up.
I stumbled upon the exact same problem this morning and fixed it rather quickly thanks to your post. I knew I would’ve spent countless of hours on troubleshooting this in IE as Firefox is way more forgiving on semantic errors like this.
On a related note I’d like to mention JSLint, which is an excellent tool for validating JS code.
Thanks again!
JSLint does look to be a pretty slick tool, although I didn’t try it. The only thing that could make it better is if you could upload an entire file rather than just cutting and pasting.
Thank you, I have wasted an hour searching for an explanation. Damn you Internet Explorer!
Kristof - Sounds like you and I were in about the same place. I’m sure it was at least an hour that I spent looking for a solution to what turned out to be such a simple issue.
Thank you so much! Ran into the exact problem myself and you just saved me hours of debugging!
You’re a lifesaver man!!!