How to decrement work in JavaScript for loop?

Your conditional check on your second for loop is incorrect. You are saying i=10, and continue while i<5, which would be never.

Try

for(var i=10; i>5; i--) { alert(i); } 

Better check it with ease... for decrement use

for (var i = 10; i > 5; i--) { alert(i); }

The first loop starts with i = 1 and increments, so that i = [1, 2, 3, 4] while i < 5. The second one starts with i=10, but the body is never executed, because it should only run when i < 5.

What you want is probably:

for (var i = 10; i > 5; i--) { alert(i); }

Tags:

Javascript