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!!!
Thank you very much and thanks to kchr: JSLint is really simplifing my life !
thank you!
Thank you, thank you, thank you – only wasted 30 mins on this then I found your tip!
I am also having this error, but its just caused by loading the site normally… can’t seem to work it out :(
IE says Line 2 Char 6999 – which doesnt exist
and the error only started occurring when we moved from one server to another.. any ideas?
I’ve had the same cause many times, but this is the first time I’ve seen this error message.
You may have been searching to page 10, but now you’re #1 for
ie javascript error “expected identifier string or number”
Many thanks!!!
Thanks,
It helped me and i was able to solve imm.
Thanks
Thanks! Had the exact same problem – this post saved me a lot of trouble!
Wow, Thanks I was dead in the water on this one. If you hadn’t taken the time to put up this post, myself and all the posters above would be stuffed.
Thanks for sharing for sharing. JSLint worked like a charm
Saludos, Yo tengo un codigo distinto (donde no veo comas) pero que me da el mismo error: “se esperaba un identificador, una cadena o un numero”. No he podido ubicar el problema y quisiera tratar de ubicar mi solucion a traves de tu ejemplo.
Mi codigo (por si acaso ayuda) es el siguiente:
[CODE]function moreFields() {
for(i = 0; ; i++) {
var state = document.getElementById(‘tr-’+i).style.display;
if (state == ‘none’) {
document.getElementById(‘tr-’+i).style.display = ”;
break;
}
}
}[/CODE]
Crees que podrias ayudarme a identificar lo que no permite que funcione en IE?
Gracias de antemano por tu tiempo y ayuda.
Saludos!
thanks from chile!
I just can say, you saved my life… because of , comma
Saved me alot of trouble, thanks :)
THANK YOU THANK YOU THANK YOU!!!
I’ve spent the last 3hours looking for a fix and this was exactly what I needed!
thanx a lots………….
Hey thanks man…Keep posting such things it helps people save lot of time…Thanks again…
Thanks for that, had the error come up in IE, sorted it out after reading your tip :D
Ross
Thank you SO MUCH for posting this! I was going NUTS trying to figure out what was wrong!
Confirmed on IE6/WinXp sp2 clean install.
Works like a charm! Great input, thanks.
So everyone, write clean code or IE will destroy you.
(that’s a bit ironic isn’t it?)
Thanks a lot. I was going mad or at least, depressed: my code worked just fine in Safari, Opera and Firefox, but IE always is the pain in the ass… innit?
Thank you so much for this. I had the exact same problem.
Thanks a million, this saved me a ton of time! Heh, IE ftl once again… This site wins! Thanks!!
Wow thanks a billion, bloody hell IE annoys the crap out of me sometimes, had it not been for your post I would have wasted half a day’s work trying to pinpoint what was throwing me off.
ahhh, Crapy IE!!!…
Thanks for that, man! You rulezZZ!
ooh great..thank you very much.thaks a lot
Awesome. I was going nuts until I found this….
Thank you for this! saved a lot of time for me as well :)
I forgot to mention.. while in IE8, working locally, the extra comma was ignored and no error was thrown. It was when I published my page the error was thrown for me.
Last reply I swear.. (in response to my last reply)
I realized why it had worked locally and not after I published. While using IE8, on the tab with my local version opened it was in Browser Mode: IE8 Compat View / Doc Mode: IE8 Standards. On the tab with the live published version opened it was in Browser Mode: IE8 Compat View / Doc Mode: IE7 Standards.
alright I’m done.
Thanks!
@Sam – No worries. I’m the same way. As soon as I hit submit I think of something else to say.
You know what’s sad? Googling for an IE7 specific JavaScript issue and having your own webpage come up :) Got this error today and forgot that I had made this post, but Google helped me remember.
I forgot to mention.. while in IE8, working locally, the extra comma was ignored and no error was thrown. It was when I published my page the error was thrown for me….
thanks a lot, first page i found when looking at the same problem!
Thanks a lot man… you are superb…
Thanks a lot man…Saw the extra comma…But dint realise that was causing the issue..
Great dude, you saved my life.
Thank you, thank you, thank you!!!
I’ll note that IE will issue this error any time it can’t parse a javascript literal, and the parsing fails when it’s trying to read a key. So it will issue this same error for:
var dict = {{a:1, b:2}};
and for
var dict = {-a:1, b:2};
in addition to for the extra comma.
Although other browsers would also choke on these errors, IE parses javascript a bit more aggressively than other browsers, so for example if you add a whole tree of html elements by setting innerHTML on a containing DIV, and some of the new elements you create have “onClick” attributes with javascript errors in them, IE will notice and complain right away. Firefox won’t notice until someone clicks and so activates the bad javascript. This means IE will throw up this error even if it’s something you don’t necessarily see in other browsers.
Also, a trailing comma is a problem in IE in arrays too, not just in dictionaries. In arrays the behavior isn’t a parse error though: you end up with a trailing addition to the array that’s “undefined”. (whereas Firefox, Safari, Chrome, etc., simply have one fewer element than IE)
You just saved me a lot of work. A ‘gotcha’ to remember for next time. Thanks!
Thanks for sharing this information . i wasted 2 hours for this issue. now it fixed thanks lot
THANKS A LOT !!
Thanks a lot.Its very useful.
Many thanks for this! Saved me hours I’m sure.
Thank you,really helped ,FF may ignore error in java script which ie didn’t ignore..learned lessons :)
Many thx 4 this dude! U are the man!
Thanks man, you saved my day!
After 1h of JS debugging and googling I slipped over your article and now I’m all clear in IE7 again. I surely learned a lesson as well today… ;)
Cheers,
Lars.
Thanks man it saved my day.
your explanantion is correct and to the point ..
Thanks thanks thanks
Husain
You are my day saver .
Huge Thanks
And by the way, I tracked the error by using firefox, It is a warning in firefox but It pointed out where is the problem, fix it and marvelous.
I stumbled across this same problem. It’s amazing what a comma can do.
Thanks heaps for your thoughtful post that saved me hours of wasted time.
I would not say it’s an error actually, I think it is allowed to have a trailing comma! Makes live so much easier. But thanks to IE7 not any more…
Thanks for sharing!
One of the very few times I have done a Google and found the exact answer first time. AND!!!! described in a way that mere mortals can understand.
Granted we should do things right and according to the coding standards…but which one!!
Thanks for sharing! My brain thanks you a lot!
Thanks a lot! Helped me solve my problem with IE7
Thankyou so much for taking the time to post this. I don’t know what I would have done if i hadn’t found this page.
Thanks for the time spent on this…. you saved me hours from my OCD in programming.
what the hack, IE always does the stupidest things puzzling developers like us
Thank you so much !!
This was in real life saver !!
Thanks, this was a life saver.
You are a life saver, thinking of sending Microsoft an invoice for unnecessary time wasting. Or file a law suite of incompetence.
Thank you very much, I had lost many time searching for the ansr!!!
THANK YOU! I spent 2 hours trying to figure out what was going wrong and I found your post. What a life saver.
thanks man saved me a lot of time. I f**king hate IE with a passion
Thanks a lot. You saved my code.
Thanks a lot. You saved my day ;-)
Thanks! Saved me lots of trouble!
what a star!!!!!
thanks for fixing this thing. there is however some of us out here that needs a geek to hold our hand. what is the actual step by step procedure to actually fix this thing?
many thanks
Thanks AGAIN!!! haha a while ago I had this puzzling error and stumbled upon this site, then today again and again Google got me here! To actually find the trailing comma is easy if you use JSLint.com, the error you’ll be looking for is something like this:
Problem at line 4 character 500: Extra comma.
@Paul
You think it’s bad that you came to this page twice. I’ve come to this page through Google looking for the cause of the “expected identifier”, and I wrote the post.
Ryan – I’m glad to see you’re still checking comments. This just saved me a potentially significant amount of frustration. ;) Thanks for posting!
Thank you so much!! I just ran into this very problem on a website I’m building and was almost tearing my hair out!!
thanks for sharing, now I can sleep well!!
Thanks for this :)
Thanks man,
you rule
:)
I am having this same problem with IE, it says “Error on page” @ the bottom of the page, when i click it, this wondow pops up it shows:
Line: 34
Char: 6137
Error: Expected Identifier
Code: 0
Now what am i supposed to do? Need help to fix this problem asap please :) it would be greatly appreciated.
I had the same problem and your post helped me fix it within 2 mins instead of trying and working around the problem for hours. Thank you very much. :)
Thanks…
i knew this solution but again i did mistake and found this particular page.. Great work Done!!!!
You saved me from spending another hour trying to figure out what the hell was going on. Thank you very much.
Thanks for sharing this tip! Your help made this a 1 minute fix instead of a major debug ordeal!
Thanks very much for sharing. I have IE had have, in the past, spent ages trying to find fixes. In one search and a few seconds later, script executing perfectly.
Thanks for saving me a huge post-weekend headache.
ah, thanks! that helped! weird that it doesn’t break chrome or firefox…
THANK YOU! Something so bloody obvious but that I’d not spotted after spending hours checking over the code :-)
THANK YOU! Saved me a while of pointless panicking!
I could kiss you! (I won’t, but I totally would!)
I had this error today and i found another case that triggers it, not the same cause as above.
value = ({default: {textAlign: ‘center’}});
this code triggers it in IE but its fine in firefox. aparently ie doesnt like the formating without single quotes.
changing it to:
value = ({‘default’: {‘textAlign’: ‘center’}});
fixes it.
cheers
Excellent, this was exactly the problem I was having. Thanks!
Thanks so much for this fix, I would have been looking for hours if it hadn’t been for you :)
Thanks buddy, u save me :)
Thank you so much for that hint. I ran into this problem while loading a secound page into a div via prototype.js. The Ajax.updater was not able to eval the returned script.
Thank’s alot mate!!!!
Thanks man. That post saved me a lot of time.
I was trying to figure what was causing my site to crash from a while now.
Best,
Alex
Thank you very much for this. I’m not sure how long it would have taken me to figure this out, but your post made it a two-minute problem.
Thanks!
Excellent post, I had a rogue comma as suspected.
Thanks again :)
DUDE. thanks.
Yes! Thank you for saving a lot of time. :)
Thank you for saving me time!
Yes! Thanks very much too … this was VERY helpful . I also had a rouge comma causing my JavaScript to fail in IE7.
It’s not an error!!! That little coma it’s suppose to be good practice! God knows what reason have the ie7 guys to implement javascript that way. Also, crappy-crappy program alert.
Thank you very much my friend!!!
Props! Thank you for your useful tip. IE has wasted many hours of my life. MS needs to get with the program. Can I get an amen…
Thanks a lot buddy, you save me a huge waste of time :D
you’re my sunshine my only sunshine!
Can have a beer for that.
Fixed on galleriffic after a day spent to try and to try…
This post saved me hours! Thanks for posting it.
I had the same problem, google it and found your blog. Went through my script and sure enough there was and extra “,”. Thank for sharing!
Thanks alot man….it got resolved…
Thank you very much! I try to find the error where is, but I failed, so your solution help me. I made a test, the error only in IE6/7, not in IE8…
Dude….THANKS for that. you. I’m glad ur sitting on Number one for my Google search… that error had the potential to ruib my whole day.
Thank you very much! I’m happy you shared how you were able to solve this problem! More power to you!
WTG dude!
I’ve been spending hours looking for solution for this issue in IE7.
IE8 ignores the extra comma as well.
hi, your post just saved hours of debuggin with this shit microsoft calls a browser. Thank you!
An absolute lifesaver. You now come up as the first result on Google for the search “expected identifier string or number”, so thank you for saving us from having to look through 10 pages (or even 2/10 of one).
This saved me alot of grief, I designed my site using IE8, FF Safari and Chrome and everything worked fine, but when I tested in IE compatability view it all went to pieces, but thanks to your post the problem was sorted in 10minutes instead of hours so a big thumbs up!
Thanks dear your solution saves lots of my time and I really appreciate you. good job and thank you very much.
Many Thanks
Hi, Thanks a lot you save my day!
Thanks for that great hint! You just helped me saving a lot of time.
Thanks you thank you thank you thank you!!
I had this error in my Jquery scrollable which made it
not work on IE7. Thank you for saving me the whole day
looking for a solution!
Cheers for the fix!
thanks to you and thanks to google that lead me to the perfect solution to my problem…cheers!!
Okay, that’s great, but I’m not a techie so I have no idea how to get to where you’re talking about to “fix” this problem…. Anybody willing to help me out……and I mean HELP…..hand-in-hand, step-by-step….
Thanks
@Carla – You’re getting this error in a .js file somewhere. The message in IE should point you to the file and at least a line near the problem. The very last line should not have a comma at the end.