3

I am on Chromium Version 53.0.2785.143 Built on Ubuntu, running on Ubuntu 16.04 (64-bit)

According to the ECMAScript® Language Specification, prefix increment operator is evaluated as follows:

Prefix Increment Operator evaluation steps

With this in mind, I cannot explain this result:

++'1';
> Uncaught ReferenceError: Invalid left-hand side expression in prefix operation

when the following code works like a charm:

var x = '1'; 
++x;
> 2

As far as I understand, in both cases the first 3 bullet points of the second step are true, whereas for ++'1' case the fourth bullet is also true (but why?) and for the ++x case it is false, raising no error. Am I right?

PS: Firefox throws a SyntaxError: invalid increment operand instead of a ReferenceError

2
  • 1
    You get the same error if you do ++1, by the way Commented Oct 12, 2016 at 13:58
  • 1
    PutValue('1', 2) throws an error because it cannot assign to a string literal. You need a variable or some other kind of Reference. Commented Oct 12, 2016 at 13:59

2 Answers 2

10

The problem is that your ++ operator implicitly involves an assignment, and you can't assign a new value to a string constant. Note that

++2;

is also erroneous for the same reason.

Sign up to request clarification or add additional context in comments.

2 Comments

…which presumably happens in 5. Call PutValue(expr, newValue)?
Oh.. I missed that out. Thanks for the answer!
4

In my understanding, ++ is similar to += 1.

So it will work for ++x as it will be evaluated to x+=1 or x=x+1, but ++'1' is a string literal and does not has left hand side value to assign, hence it fails

3 Comments

You can even literally do '1' = Number('1')+1 and will get the same error
Because left hand side value is not valid. you cannot have '1' as variable name
Oh.. I missed that out. Thanks for the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.