While writing a swap function using chained shorthand operators in javascript, I stumbled upon something which puzzled me a lot.
This code is supposed to swap a and b values.
var a = 4532;
var b = 969;
a^=b^=a^=b;
But it doesn’t: b = 4532, but a = 0.
If I break this code in 2 lines, it works as intended.
var a = 4532;
var b = 969;
b^=a^=b;
a^=b;
What is the technical explanation?
PS: here’s the code on jsfiddle if someone wants to quickly try for himself.
Before answering your question, can you tell the result of the following code?
You may believe that it’s 4, try it yourself 🙂
I have no idea of how the javascript code is interpreted under the hood. Here is how I try to explain why that happened.
By
a += b, it is equal toa = a + b. So,c += c += 1isc = c + (c = c + 1). Assignment in form of(c + (c = c + 1)), the result is(c + c + 1). Here I think it is the key point, and it’s confusing, the variablecis still1, although we reassign a new valuec + 1to it, in the first assignmentc += 1.So,
a^=b^=a^=bis equal toa = a^b^a^b, that’s0. Because the internal change toais ignored, or maybe it’s a bug?My another guess is that, the code may be expanded like this:
That’s all my guesses, cause I don’t know how the bytecode generated by VM looks like. Hopes it is useful.