That's because they both stop when i
reaches value of 7. Let's go over every single iteration, shall we?
During first iteration of the loop you:
- start with
i=0
, then you add two zeros together with i+=i
, and finally increase it by 1 via i++
, you end up with i=1
During second iteration of the loop you:
- start with
i=1
, they you add two 1s together with i+=i
, and finally increase it by 1 via i++
, you end up with i=3
During final iteration of the loop you:
- start with
i=3
, they you add two 3s together with i+=i
, and finally increase it by 1 via i++
, you end up with i=7
Finally the console.log(i+=i)
adds two 7s together and you get 14.
Both loops stop at the 3rd iteration when i reaches the value of 7
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…