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.

JavaScript:
  1. var variableName =
  2.     {
  3.     function1:   function()
  4.         {
  5.         //  Do something
  6.         },
  7.     function2:   function()
  8.         {
  9.         //  Do something else
  10.         },
  11.     variable1:    'Some value',
  12.     variable2:    'Another value',
  13.     };

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.

68 Responses to “JavaScript Error ‘expected identifier, string or number’ in IE”

  1. 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!

  2. 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.

  3. Thank you, I have wasted an hour searching for an explanation. Damn you Internet Explorer!

  4. 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.

  5. Thank you so much! Ran into the exact problem myself and you just saved me hours of debugging!

  6. You’re a lifesaver man!!!

  7. Thank you very much and thanks to kchr: JSLint is really simplifing my life !

  8. thank you!

  9. Thank you, thank you, thank you – only wasted 30 mins on this then I found your tip!

  10. 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?

  11. 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!!!

  12. Thanks,

    It helped me and i was able to solve imm.

    Thanks

  13. Thanks! Had the exact same problem – this post saved me a lot of trouble!

  14. 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.

  15. Thanks for sharing for sharing. JSLint worked like a charm

  16. 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!

  17. thanks from chile!

  18. I just can say, you saved my life… because of , comma

  19. Saved me alot of trouble, thanks :)

  20. THANK YOU THANK YOU THANK YOU!!!

    I’ve spent the last 3hours looking for a fix and this was exactly what I needed!

  21. thanx a lots………….

  22. Amit Sidhpura on June 22nd, 2009 at 5:54 am

    Hey thanks man…Keep posting such things it helps people save lot of time…Thanks again…

  23. Thanks for that, had the error come up in IE, sorted it out after reading your tip :D

    Ross

  24. Thank you SO MUCH for posting this! I was going NUTS trying to figure out what was wrong!

  25. 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?)

  26. 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?

  27. Thank you so much for this. I had the exact same problem.

  28. Thanks a million, this saved me a ton of time! Heh, IE ftl once again… This site wins! Thanks!!

  29. 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.

  30. ahhh, Crapy IE!!!…
    Thanks for that, man! You rulezZZ!

  31. ooh great..thank you very much.thaks a lot

  32. Awesome. I was going nuts until I found this….

  33. Thank you for this! saved a lot of time for me as well :)

  34. 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.

  35. 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.

  36. Thanks!

  37. @Sam – No worries. I’m the same way. As soon as I hit submit I think of something else to say.

  38. 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.

  39. 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….

  40. thanks a lot, first page i found when looking at the same problem!

  41. Thanks a lot man… you are superb…

  42. Thanks a lot man…Saw the extra comma…But dint realise that was causing the issue..

  43. Great dude, you saved my life.

    Thank you, thank you, thank you!!!

  44. 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)

  45. You just saved me a lot of work. A ‘gotcha’ to remember for next time. Thanks!

  46. Thanks for sharing this information . i wasted 2 hours for this issue. now it fixed thanks lot

  47. THANKS A LOT !!

  48. Thanks a lot.Its very useful.

  49. Many thanks for this! Saved me hours I’m sure.

  50. Thank you,really helped ,FF may ignore error in java script which ie didn’t ignore..learned lessons :)

  51. Many thx 4 this dude! U are the man!

  52. 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.

  53. Thanks man it saved my day.

    your explanantion is correct and to the point ..

    Thanks thanks thanks

    Husain

  54. You are my day saver .
    Huge Thanks

  55. 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.

  56. I stumbled across this same problem. It’s amazing what a comma can do.

  57. Thanks heaps for your thoughtful post that saved me hours of wasted time.

  58. 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!

  59. 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!

  60. Thanks a lot! Helped me solve my problem with IE7

  61. 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.

  62. Thanks for the time spent on this…. you saved me hours from my OCD in programming.

  63. what the hack, IE always does the stupidest things puzzling developers like us

  64. Thank you so much !!

    This was in real life saver !!

  65. Thanks, this was a life saver.

  66. You are a life saver, thinking of sending Microsoft an invoice for unnecessary time wasting. Or file a law suite of incompetence.

  67. Thank you very much, I had lost many time searching for the ansr!!!

  68. THANK YOU! I spent 2 hours trying to figure out what was going wrong and I found your post. What a life saver.

Leave a Reply