- 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.
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.
For-in loop with hasOwnProperty check, iterates over properties of the object.
- Lodash _.forIn() Method
- 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 firstname.lastname@example.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.