Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does false++ produce a SyntaxError in Firefox but a ReferenceError in Chrome?

While trying to debug some faulty piece of JavaScript, I found a line that looks like an obvious mistake in a source file:

false++;

What I don't undestand is why this statement behaves differently in all browsers.

  • In Chrome, I get a ReferenceError and the whole script is not run.
  • In Firefox, I get a SyntaxError and the whole script is not run.
  • In Internet Explorer, I get a SyntaxError and the script runs only until the line where the error occurs.

Is it by design that different browsers are allowed to handle the same broken JavaScript in different ways?

I know what the error is and how to fix it, but shouldn't at least the error type be mandated by the spec?

like image 781
Lynn Avatar asked Sep 14 '16 10:09

Lynn


1 Answers

Chrome appears to be up to date.

  • In ES6 and ES7 a ReferenceError is thrown when trying to assign to a primary expression that is a literal (such as false) not an identifier.
  • In ES5 an early error (which usually are SyntaxErrors) of unspecified type is thrown for assignments "on any value for which an early determination can be made that the value is not a Reference", though if it would happen a ReferenceError would be thrown so one might argue that the early error should be of that type as well.
  • In ES3, the description of the error condition is similar to ES5, but it only states that "an implementation may [sic!] treat any instance of the following kinds of runtime errors as a syntax error and therefore report it early".
  • In ES1 and ES2 there were no error types and no exception handling at all, and assigning to something that is not a reference only is said to "generate a runtime error". Implementations were allowed to report them early at compile time though, if they could prove that the error would happen under any circumstances.

While the error handling in Firefox may be excused by the ES5 or ES3 wording, the behaviour Internet Explorer throwing a runtime SyntaxError does not match any of these. However, Microsoft plans to fix this in Chakra. For further discussion, see https://github.com/tc39/ecma262/issues/257 and https://github.com/tc39/ecma262/issues/691.

like image 183
Bergi Avatar answered Oct 16 '22 09:10

Bergi