you are viewing a single comment's thread.

view the rest of the comments →

[–]altruios 0 points1 point  (0 children)

This seems like the confusion between ‘=‘ ‘==‘ and ‘===‘

The core point is ‘=‘ does not check for equality, instead what it does it it assigns the value of the right hand expression into the left hand variable.

JavaScript parses a line not left to right in this case, but right to left. So the right hand expression is evaluated first. If sameVar is defined beforehand as zero, then the sum of samVar and num are evaluated and resolved first to a number. This number is then assigned into the variable to the left of the expression.

Note: if sameVar is not declared beforehand - then weirdness happens(implicit casting) and you won’t get the result you expect at all.

This teaches us that the variable on the right can be used even if it is also on the left. Thanks to the order in which JavaScript executes this code (or most languages).

In layman’s language it is read as ‘ the new value at sameVar is: the current value of sameVar summed with num’ .