JavaScript (JS) – for loop

for (let number = 0; number <= 12; number = number + 2) {
  console.log(number);
}

Result :" 0 2 4 6 8 10 12"

How is 0 Printed?
On the loop;
var "number" is declared as 0 and it is lower than 12
so it should be added by 2. So, 0 + 2 = 2
and only it is printed, and the loop is repeated.
So, how is 0 printed?

>Solution :

The loop starts with number as 0. The condition checks if the number is less than or equal to 12, which it is. So, 0 is printed because it meets the condition before the loop progresses to adding 2 to the number.

If you start the loop at 2, that’s the first number printed. Each time, it adds 2 to the previous number until it reaches or exceeds 12.

Leave a Reply