Skip to content

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 and reviewing 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 review code and finding the fix than I did.

Quick edit, thanks to Pete in the comments. Looks like using a reserved word will also cause the same error in some versions of Internet Explorer. –Ryan

Published inCoding

297 Comments

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

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

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

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

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

  8. Ryan Ryan

    THANK YOU THANK YOU THANK YOU!!!

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

  9. Amit Sidhpura Amit Sidhpura

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

  10. D Barnes D Barnes

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

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

  12. pia pia

    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?

  13. bak bak

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

  14. cmoslim cmoslim

    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.

  15. Sam Sam

    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.

  16. Sam Sam

    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.

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

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

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

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

  21. husain husain

    Thanks man it saved my day.

    your explanantion is correct and to the point ..

    Thanks thanks thanks

    Husain

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

  23. Jörg Ramb Jörg Ramb

    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!

  24. Graeme Lewis Graeme Lewis

    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!

  25. Harry Harry

    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.

  26. Martin Martin

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

  27. Josh Josh

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

  28. doug doug

    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

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

  30. @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.

  31. Ryan – I’m glad to see you’re still checking comments. This just saved me a potentially significant amount of frustration. ;) Thanks for posting!

  32. webbuilder webbuilder

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

  33. Amy Amy

    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.

  34. yasar yasar

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

  35. sonam sonam

    Thanks…

    i knew this solution but again i did mistake and found this particular page.. Great work Done!!!!

  36. Alex Forster Alex Forster

    You saved me from spending another hour trying to figure out what the hell was going on. Thank you very much.

  37. Keith Keith

    Thanks for sharing this tip! Your help made this a 1 minute fix instead of a major debug ordeal!

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

  39. malificent malificent

    ah, thanks! that helped! weird that it doesn’t break chrome or firefox…

  40. Rob Rob

    THANK YOU! Something so bloody obvious but that I’d not spotted after spending hours checking over the code :-)

  41. Kevin Kevin

    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

  42. Alexander Alexander

    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.

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

  44. MarkHebs MarkHebs

    Yes! Thanks very much too … this was VERY helpful . I also had a rouge comma causing my JavaScript to fail in IE7.

  45. Teo Teo

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

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

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

  48. Brad Brad

    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!

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

  50. Taz Taz

    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.

  51. Justn Cribb Justn Cribb

    Thank you very much! I’m happy you shared how you were able to solve this problem! More power to you!

  52. Jerry Jerry

    WTG dude!

    I’ve been spending hours looking for solution for this issue in IE7.
    IE8 ignores the extra comma as well.

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

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

  55. Jatin Jatin

    Thanks dear your solution saves lots of my time and I really appreciate you. good job and thank you very much.

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

  57. Carla Carla

    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

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

  59. ihaskin ihaskin

    First Google result, and your explanation was EXACTLY what has been driving me bonkers.

    A hint:

    I was using php-includes, so when I looked for the line number that the error was referring to, nothing lined up –> load the page in IE and then view the entire source of the page. Copy and paste into a text editor that shows line numbers (or count them).

  60. Nandish Nandish

    Thanks a lot. I had implemented a rather complicated JQuery based grid which was working fine with Firefox and Chrome. It was IE that threw an exact error. Thanks to this post, I solved my problem.

  61. Kate Kate

    Just adding to the chorus! Thankfully, I’d googled first, so I only looked in two places before I found the comma that was breaking IE7 and not IE8!

  62. Marvin B. Aya-ay Marvin B. Aya-ay

    What a monster IE small mistake is not allowed.
    By the way thanks to your post you save big pain and my time.

    Cheeerzzz……

    Thank you very much.

  63. fei fei

    Many thanks!
    you save my life too!! This error appears only below version IE8.
    I think IE8 automatically ignore this comma :)
    P.S.: Hate IE, HATE! :)

  64. JPlayer JPlayer

    GUUHHHHHH!!! Thank you SO MUCH for posting this. I spent about an hour trying to figure out why IE was being so picky (would’ve helped to know the correct line number…IE was WAY off). This is EXACTLY what my problem was. THANK YOU!

  65. prashanth prashanth

    Thanks for your valuable post….
    I had the same problem and looking into ur post i have rectified it within a minute.. Thanks a lot…..

  66. Finally solved my issue with this after stumbling upon your post. Thanks. Oddly, fixing this error in IE7 also corrected a different but equally frustrating problem in IE8 (which didn’t actually generate a JavaScript error).

  67. imtiyaz imtiyaz

    Thank you very very very very very very very very very veryvery very very very veryvery very very very veryvery very very very veryvery very very very veryvery very very very veryvery very very very veryvery very very very veryvery very very very veryvery very very very very MUCH……….

  68. Ashok Ashok

    Thanks a lot, I am also getting stuck on the same type of problem, after reading this “JavaScript Error ‘expected identifier, string or number’ in IE” blog.

    I got the solutions.

    Thanks,
    Ashok

  69. RockerNJ RockerNJ

    Thanks so much! 1st hot on Google for the problem and picked off the problem exactly!

  70. Ankita Ankita

    AWESOME!

    Works for me too.. Had a similar exception, and thankfully saw your post soon enough to fix it in 5 minutes :)

    -Ankita

  71. Bdawg Bdawg

    Just wanted to say a quick thanks for taking the time to jot this down. Just helped me too.

  72. Barry Barry

    Thank you! I’ve spent the last 3 hours scouring through code to find this simple solution. I added a list of parameters into a shadowbox js and kept only one but left the comma.

    Don’t know that I would have found it without this post! Thanks again!

  73. Kevin Kevin

    Thanks a ton for posting this. Probably saved me a few hours of headache this morning on a work project! :)

  74. Lukas Lukas

    Hey man thank you this help me a lot ! such a stupid comma :D I spend two hours looking where i have a mistake a than i found this post. :)

  75. Rohmel Rohmel

    Like many before me, thanks for saving me the time of troubleshooting this issue with the bane of the web developers world, IE.

  76. dipak kumar burnwal dipak kumar burnwal

    hey thanks dude you really pull me out from hell i was searching for the solution for last 3 days and it was only today that i got solution
    thanks …..

  77. Tarun Tarun

    Thank you!!! I moved a block of code from one part of the file to another and was wondering why did it start breaking all of a sudden.

    the trailing comma!

  78. Adel Adel

    Exact problem with TinyMCE was only in IE9 com mode, they become more forgiving;

    toke me couple of mins thanks to you.

  79. adam adam

    You just saved me, what could of been, hours of pain. Thank you! It’s amazing that 3 years on IE is still this bad. It drives me nuts. Thanks again buddy!

  80. I came here by way of Google and you’ve saved me a huge headache trying to get TinyMCE text editor to work on IE 8 and older browsers!

    Thanks for this post!

  81. Todd Todd

    Damn you for not getting me to read your page sooner. I’m joking. Thanks for your article. I had exactly the same problem… but I probably wasted at least 30 minutes trying to debug it. Cheers!

  82. Thanks for publishing, pointing this syntax error out. I discovered this error through IE compatibility mode. I don’t blame Internet Explorer at all; it’s just lack of experience on my part, and you have just doubled my knowledge of JavaScript semantics.

  83. Brendan Brendan

    Thanks so much for this post! Just had the same problem and fixed it in 10 seconds thanks to this! You’re still helping people 3 years later!

  84. Wish I would have found this page sooner. That little comma was causing major problems in IE 7, making very important pieces of my website not function.

  85. was trying to get the iframe to change parent text [the count of items] in the store at this page for 3 days. Thank you !!!! for your help

    function changeText(newcount){
    var newcount = “php echo variable”;
    window.parent.document.forms[1].elements[0].value = ‘Total Items: ‘+ newcount;
    }
    {edited}

  86. Jana Jana

    This works like a charm, still can’t believe that one stupid comma was causing so many problems. Thank you so much !!!!

  87. ira ira

    wow. i rarely comment on these forums but that was so helpful i just had to say thanks. probably would have taken forever to find that fix!! :)

  88. Robertino CUcaracho Robertino CUcaracho

    This are the little things that makes me hate Microsoft a little more every day. =D

    Thanks! I was just testing IE compatibility and found this errors all over the place. Damn it. XD

  89. pete pete

    reserved words will also produce the same error/effect.

    e.g. :
    var x = {
    class: “a”
    };

    IE8 (9 ok)

  90. sulfide sulfide

    please don’t take this down, it’s still 2012 and I have to deal with IE8 (just starting doing some web developing) and this definitely saved me some time…!

    Thank you!

  91. nazeer nazeer

    plot1 = $.jqplot(‘chart1’,[cosPoints],{
    title: ‘Highlighting, Dragging, Cursor and Trend Line’,
    axes: {
    xaxis: {
    renderer: $.jqplot.DateAxisRenderer,
    tickOptions: {
    formatString: ‘%m/%d/%Y’
    },
    numberTicks: 4
    },
    yaxis: {
    tickOptions: {
    formatString: ‘%d’
    },
    min: 0,
    max:400,

    },
    },
    highlighter: {
    sizeAdjust: 10,
    tooltipLocation: ‘n’,
    tooltipAxes: ‘y’,
    tooltipFormatString: ‘hello %.2f’,
    useAxesFormatters: false
    },
    cursor: {
    show: true
    }

    this is my jquery jqplot plug in graph , its working perfectly in firefox but in ie i got error “Expected identifier, string or number” in line number 88 but my line number 88 is formatString: ‘%m/%d/%Y’ so please can any one tel me the solutin what i should do please tel me if anyone known this

    • Looks like you have an extra comma after the max field on your y-axis. Remove the comma after Max: 400 and you should be good to go.

  92. Thanks, you rock.

    This was my problem, too. I was actually explicitly leaving the last comma in, so that there wouldn’t be “accidents” if people copy/paste lines of code, as they’d all end in commas.

    Now I know to avoid doing that for the plebs that haven’t upgraded from IE yet.

  93. Robin Robin

    In case anyone can’t seem to find the code that causes the error here is a little RegEx snippet you can use:
    “,[\s]+?\n?[\s]+?}”
    it should find all occurances of “,[spaces/tabs and/or new line]}”

    I used it in combination with the Aptana Eclipse plugin which has a wonderfull (and extemely usefull) ‘Search in all files’ function that supports

  94. wakaso wakaso

    thanks a lot, the only thing confusing me for hours just a comma ‘,’. After remove it, it works like a jerk.

  95. Vega Vega

    YOU ARE AWESOME!!!!! You saved me countless hours of javascript debugging. Thanks for finding reason 2,993,882,581 why I hate IE :)

  96. kelly kelly

    Webpage error details

    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
    Timestamp: Wed, 15 May 2013 07:37:04 UTC

    Message: Expected ‘:’
    Line: 78
    Char: 525
    Code: 0
    URI: https://www.google.com.au/

    Can someone please tell me how to fix this error im a total novice so any help appreciated. Many Thanks Kelly

  97. a a

    Thanks for ones marvelous posting! I genuinely enjoyed
    reading it, you could be a great author.
    I will make certain to bookmark your blog and will come back very soon. I want to encourage you continue your
    great job, have a nice day!

  98. Mikaere Mikaere

    I had the same problem but the solution wasn’t the same. It was because I had used a JavaScript reserved word as a variable. I didn’t know that the word I used was reserved. I changed the name of the variable and error was resolved.

  99. ehtishamyounas ehtishamyounas

    $(document).ready(function() {
    $(“#aspnetForm”).validate({
    rules: {
    : {
    minlength: 2,
    required: true
    },
    : {
    required: true,
    email:true
    }
    }, messages: {
    : {
    required: “* Required Field *”,
    minlength: “* Please enter atleast 2 characters *”
    }
    }
    });
    });

    myjavascript code show error “expecting identifier , string or number”

Leave a Reply

Your email address will not be published. Required fields are marked *