I am learning JavaScript for loop where I tried running this code in google chrome developers tools where the output made me more confused. Can someone explain me what this code is doing.
let sum = 0;
for (let i = 2; i <= 6; i++) {
sum = sum + i;
}
I was expecting the result as 6, as the "test condition" given is i <= 6 but I got the output as 20. How I am getting 20 here when the loop has to stop at 6
i, and 2 + 3 + 4 + 5 + 6 is 20sumto read6, you should increment by 1 at every iteration, i.e.sum++instead ofsum + i.5since it starts at2.let i = 1; i <= 6orlet i = 0; i < 6in conjunction with usingsum++then that will work