JavaScript concating with number as string -
why x = "1"- -"1" work , set value of x 2?
why doesn't x = "1"--"1" works?

this expression...
"1"- -"1" ... processed ...
"1"- (-"1") ... is, substract result of unary minus operation applied "1" "1". now, both unary , binary minus operations make sense applied numbers - js converts operands numbers first. that'll becomes:
number("1") - (-(number("1")) ... that'll becomes evaluated 2, number("1"), expect, evaluates 1.
when trying understand "1"--"1" expression, js parser attempts consume many characters possible. that's why expression "1"-- processed first.
but makes no sense, auto-increment/decrement operations not defined literals. both ++ , -- (both in postfix , prefix forms) should change value of assignable ('left-value') expression - variable name, object property etc.
in case, however, there's nothing change: "1" literal always "1". )
actually, got bit different errors (for x = "1"--"1") both in firefox:
syntaxerror: invalid decrement operand ... , chrome canary:
referenceerror: invalid left-hand side expression in postfix operation and think these messages show reason of error quite clearly. )
Comments
Post a Comment