Uncaught TypeError: iterable is not iterable

The issue:

In our project when we tried to loop over an object using for..of loop in JavaScript we got into the following issue:

Uncaught TypeError: iterable is not iterable

Following is the code that triggered the issue:

let obj = {name: 'Joe Smith', age: 24};
for (let value of obj) {
    console.log(value);
}

The reason of the issue:

The for..of loop in JavaScript only works with iterable object. In the above code the variable obj is not an iterable object. As it is not an iterable object, it throws an uncought typeError.

The solution:

for..in loop is designed to loop over objects in JavaScript. We had to use for..in loop instead of for..of loop. As soon as we used for..in loop the issue was resloved. See the working code below:

let obj = {name: 'Joe Smith', age: 24};
for (let index in obj) {
    console.log(obj[index]);
}
Share