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?

  • What does the spec say? – Teemu Sep 14 '16 at 10:42
  • 3
    @Teemu I don't know. Do you? – Lynn Sep 14 '16 at 10:44
  • 3
    It should be a reference error, see ECMA-262 §12.4.1: It is an early Reference Error if IsValidSimpleAssignmentTarget of LeftHandSideExpression is false. The postfix operator attempts to modify the value, false is immutable so isn't a valid simple assignment target. – RobG Sep 14 '16 at 10:48
  • @Teemu: How is that information useful? – BoltClock Sep 14 '16 at 12:28
up vote 5 down vote accepted

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.

Just a guess, but maybe Firefox checks for syntax errors first, & Chrome checks reference errors first. The first error is thrown, & goes no further. The main reason for the error is that false is a Boolean, not an Integer or numeric value, so it can't be incremented or decremented. Without seeing the source code, I'd try changing false to something like var falseCount.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.