Variable a is decared in the main file. Then a.js and b.js included. JS files have following content.
a.js:
a+=100;
b.js:
a=+200;
And the main file:
<script type="text/javascript">
     a=30;
</script>
<script type="text/javascript" src="js/a.js"></script>
<script type="text/javascript" src="js/b.js"></script>
    a+=90;
    console.log("a = " + a);
</script>
Console shows a=290. Why is  a  not 420 (i.e. 30+100+200+90 ) ? why doesn't a change across both a.js and b.js?
You wrote the += operator backwards. Written as =+ the + is being interpreted as the unary + operator; in other words, you're explicitly assigning "positive 200" to the variable (in "b.js").
You have a typo with a =+ 200; This assigns the global variable a the value 200.
I believe you meant a += 200;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With