0
var dollar = '$';
for (var i=1; i <= 6; i++) {
    console.log(dollar);
    dollar += '$';
}

I just start learning javascript, please help to explain why the script can print like this. Thx for help

$
$$
$$$
$$$$
$$$$$
$$$$$$
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Arron Lam
  • 9
  • 1
  • 9
    What don't you understand? In each iteration of the loop, it prints out the current value of `dollar`, which is some number of `$` signs. Also, in each iteration, the loop adds one more `$` to end of `dollar`, so it keeps growing by one. – Tim Biegeleisen Aug 26 '18 at 13:32
  • 5
    Welcome to Stack Overflow! Please take the [tour] (you get a badge!), have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) What is it about the code that you don't understand? – T.J. Crowder Aug 26 '18 at 13:33
  • I don't understand this line " dollar += '$'; " if I skip this line, it will turn to 6 times of $$$$$$ – Arron Lam Aug 26 '18 at 13:43
  • If you remove `dollar += '$';` it should print `$` six times, not `$$$$$$` six times – Luca Kiebel Aug 26 '18 at 13:47
  • 3
    Possible duplicate of [How does += (plus equal) work?](https://stackoverflow.com/questions/6826260/how-does-plus-equal-work) – Ivar Aug 26 '18 at 13:52
  • ok, I got confuse about chrome console. Thx for help! – Arron Lam Aug 26 '18 at 14:18

1 Answers1

1
dollar += '$'; 

is equal to

dollar = dollar + '$'; 

so first round dollar is equal to "$", so you will log it as it is, then you add another dollar to it. in next round you will see it "$$". like wise it comes to 6 dollar signs at the end, because you iterate it up to 6.

LahiruTM
  • 638
  • 4
  • 16