0

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
  • 93
  • 5

1 Answers1

6

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
  • 376,160
  • 25
  • 347
  • 392
  • 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