Why does this gives me an infinite loop

hey guys, so i was making an array from which i could toggle if it was completed with an “x” otherwise with " " , but something caught my attention

var todoList = {
todos: [],
displaytodos: function() {
if (this.todos.length === 0) {
console.log(‘your todos list is empty!’);
} else {
console.log(‘My todos:’);
for (var i = 0; i < this.todos.length; i++){
if(this.todos[i].completed === true) {
console.log(’(x)’, this.todos[i].todoText);
} else {
console.log(’( )’, this.todos[i].todoText);

if i write it down like that it comes out just fine, but when i change the final expression in my for loop to:

for (var i = 0; i < this.todos.length; i++)
to this:
for (var i = 0; i < this.todos.length; i+2)

it becomes an infinite loop, i can’t seem to understand why that happens.
i mean i can just set it to i++ and it will do what i want, but i still would like to know why does it become infinite when i only change the (i++ to i + 2)

i + 2 does not change the value of i, so the for loop condition will keep evaluating with i = 0.

1 Like

oh okay, i get it now,thank you very much!