1

I was reading about Javascript operators precedence over here and got curious why I can't write something like this:

let num = 1;
++num++;

Which gets Uncaught ReferenceError: Invalid left-hand side expression in prefix operation error. But why is that? :)

Aurimas
  • 91
  • 5

1 Answers1

7

It evaluates as

++(num++) 

so, the expression

num++

returns a number, not the variable, because it is a primitive value. The added plusses, throws an exception, because a primitive value is not a variable and an assigment is not possible.

Nina Scholz
  • 351,820
  • 24
  • 303
  • 358
  • Yes, you are right! Just `num++` is evaluated first, but the essence remains the same. Thanks, my curiosity needs satisfied! :) – Aurimas May 17 '17 at 11:19