Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading