Ah yes. It has been awhile. I knew that looked familiar.
So, the examples you referenced make sense, but I still don’t get the original example that I posted. In the 2 examples that you showed the variables had already been declared with the part of the value.
In the example I posted the value is declared after txt+=
Why couldn’t they just do this and leave the plus out?
Yea. I get what it means at this point, but it is funny to me that the last one doesn’t override the previous ones since it still means txt = a + b. I’d be interested to know the rule that makes it so that it doesn’t override like this one does
No it doesn’t. The += essentially means (in your parlance) txt = txt + a + b. When dealing with a string, it takes the value currently in the string and APPENDS the value after the += symbol. It’s the javascript equivalent of .= in php
It is setting txt equal to txt PLUS something each time.
for example:
txt = 'a';
txt += 'b';
so after the first statement txt contains ‘a’.
Since the second statement is equivalent to txt = txt + 'b' and txt contains ‘a’ the second statement is equivalent to txt = 'a' + 'b' and so now txt contains ‘ab’ and not just ‘b’.
If the second statement were txt = 'b' instead of txt += ‘b’` then txt would contain ‘b’ instead of ‘ab’.
Yes it is setting txt to something each time but the += retains the value already there and concatenates the new value to it (for strings - with numbers it would add the new value to the existing one).