Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plus equal misunderstanding

I'm trying something in my browser and I don't understand the result.

When I type :

var testPlus = 0;
testPlus += 2
console.log(testPlus)

It gives testPlus = 2.

But when I type

var testPlus = 0;
(testPlus +=2) * 2;
console.log(testPlus)

It still gives 2, like if "*2" weren't calculated. I don't get why ?

Thank you


1 Answers

You are only assigning the +2 back to testPlus. The *2 is happening, but the answer is not being stored anywhere.

So, (testPlus +=2) * 2; bumps up the value being stored in testPlus to 2 and then that is multiplied by 2, creating a value of 4. But, that 4 isn't used or stored anywhere.

like image 70
Scott Marcus Avatar answered Nov 24 '25 00:11

Scott Marcus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!