For-in loop in JavaScript is used to iterate over properties of an object. It can be a great debugging tool if we want to show the contents of an object. The for-in loop iterates only over those keys of an object which have their enumerable property set to “true”. The key values in an object have four attributes (value, writable, enumerable, configurable). Enumerable when set to “true” means that we can iterate over that property. You can read about the four key attributes in the property attributes section of Objects in JavaScript. Read more on enumerable Enumerable Properties in JavaScript.
Important points:
- Use the for-in loop to iterate over non-array objects. Even though we can use for-in loop for array, it is generally not recommended. Instead, use a for loop for looping over an array.
- The properties iterated with for-in loop also includes the properties of the objects higher in the Prototype chain.
- The order in which properties are iterated may not match with the properties that are defined in the object.
Syntax:
for (let i in obj1) { // Prints all the keys in // obj1 on the console console.log(i); } |
Example:
<script> const courses = { // Declaring a courses object firstCourse: "C++ STL", secondCourse: "DSA Self Paced", thirdCourse: "CS Core Subjects" }; // Creating a new empty object with // prototype set to courses object const student1 = Object.create(courses); // Defining student1 properties and methods student1.id = 123; student1.firstName = "Prakhar"; student1.showEnrolledCourses = function () { console.log(courses); } // Iterating over all properties of // student1 object for (let prop in student1) { console.log(prop + " -> " + student1[prop]); } </script> |
Console Output:
For-in loop iterates over properties of an object and it’s prototype chain’s properties.
If we want to display only properties of the “student1” object which belongs to that object only and not on the prototype chain, then we can perform an “if” check with hasOwnProperty() method.
<script> // Iterating over only those properties // of student 1 object which is not on // its prototype chain for (let prop in student1) { if (student1.hasOwnProperty(prop)) { console.log(prop + " -> " + student1[prop]); } } </script> |
Console Output:

For-in loop with hasOwnProperty check, iterates over properties of the object.
Recommended Posts:
- Lodash _.forIn() Method
- JavaScript | While Loop
- JavaScript | For Loop
- How to use loop through an array in JavaScript?
- How to break nested for loop using JavaScript?
- How to add a delay in a JavaScript loop?
- How to trigger setInterval loop immediately using JavaScript ?
- How to ignore loop in else condition using JavaScript ?
- What are the microtask and macrotask within an event loop in JavaScript ?
- JavaScript SyntaxError - A declaration in the head of a for-of loop can't have an initializer
- HTML | DOM Video loop Property
- PHP | Separate odd and even elements from array without using loop
- Node.js REPL (READ, EVAL, PRINT, LOOP)
- Iterate associative array using foreach loop in PHP
- Determine the first and last iteration in a foreach loop in PHP?
- How to remove an array element in a foreach loop?
- Asynchronous Functions and the Node Event Loop
- How to loop through an associative array and get the key in PHP?
- HTML | DOM Audio loop Property
- HTML | loop Attribute
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


