Interesting error reporting difference in Chrome and Firefox

This post is more than 2 years old.

Ok, perhaps "interesting" is a bit of a stretch, but as I'm doing some work while waiting for new tires to be put on my car, my base level for "interesting" is a bit lower than normal. I'm testing some code that I wrote in a Powerpoint slide - code I was sure worked fine but I wanted to be really sure, and I found that I had a typo. Consider the snippet below - you will probably see it right away.


$.getJSON("/data.json", function(res) {
	var s = "<ul>";
	//["ray","jeanne","jacob","lynn","noah","maisie"]
	for(var i=0;i<res.length;i++) {
		if(res[i].length < 4) {
			s+="<li class=\"short\">"+res[i] + "</li>");
		} else {
			s+="<li>"+res[i] + "</li>");
		}
	}
	s += "</ul>";
	$("#someDiv").html(s);
});

When I ran this in Firefox, I got: SyntaxError: missing ; before statement test1.html:22.

Line 22 is the line that adds the li with class short to the variable s. I looked at it and couldn't quite figure out what was wrong.

I then switched to Chrome and got this error: Uncaught SyntaxError: Unexpected token )

Now - that made sense! I immediately saw the extra ) I had at the end of my line. (I also had it two lines later.)

But what I found interesting is how different the errors were. Both were syntax errors, but the actual detail was quite different. Anyone have an idea as to why this is?

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by pmuellr posted on 2/14/2015 at 10:29 PM

because parsers. Everyone has their own parser. Some parsers have better error messages than others.

Safari used to use a parser built with bison, which could only ever report "syntax error" with the line number, but never more specific than that. They are using a better parser now, and get better error messages. But back then ... sheesh!

Comment 2 (In reply to #1) by Raymond Camden posted on 2/15/2015 at 4:39 PM

I get that some parsers will have "better" error messages, but these two messages seem almost contradictory. It is just plain weird. :)

Comment 3 by Joel Cox posted on 2/16/2015 at 2:11 PM

The first line of code that sets the variable "s" with the odd line break between the quotes may play into it.

Comment 4 (In reply to #3) by Raymond Camden posted on 2/16/2015 at 2:14 PM

That's just a bad paste - fixing now.

Comment 5 (In reply to #4) by Raymond Camden posted on 2/16/2015 at 2:14 PM

Fixed - thanks Joel.