What are the differences between for…of and for…in statements

Posted by Jennifer | Updated on

Both for…in and for…of statements iterate over js data structures. The only difference is over what they iterate:

  1. for..in iterates over all enumerable property keys of an object
  2. for..of iterates over the values of an iterable object.

Let’s explain this difference with an example:

Since for..in loop iterates over the keys of the object, the first loop logs 0, 1, 2 and newProp while iterating over the array object. The for..of loop iterates over the values of a arr data structure and logs a, b, c in the console.


If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.

Related Questions & Answers