🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for...in
for...in - JavaScript | MDN - MDN Web Docs
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.
🌐
W3Schools
w3schools.com › js › js_loop_for.asp
JavaScript for Loop
JS Examples JS HTML DOM JS HTML ... Interview Prep JS Bootcamp JS Certificate JS Reference ... For Loops can execute a block of code a number of times....
🌐
W3Schools
w3schools.com › js › js_loop_forin.asp
JavaScript For In
The for...in loop is primarily used for objects to access their property names (keys). for (key in object) { // code block to be executed } key A variable that holds the name (key) of each property during the iterations · object The object ...
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Loops_and_iteration
Loops and iteration - JavaScript - MDN Web Docs - Mozilla
A for loop repeats until a specified condition evaluates to false. The JavaScript for loop is similar to the Java and C for loop. ... The initializing expression initialization, if any, is executed. This expression usually initializes one or more loop counters, but the syntax allows an expression ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › javascript fundamentals
Loops: while and for
If you came to this article searching for other types of loops, here are the pointers: See for…in to loop over object properties.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Statements › for
for - JavaScript | MDN - MDN Web Docs - Mozilla
The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › for-in-loop-in-javascript
JavaScript For In Loop - GeeksforGeeks
The for...in loop in JavaScript is used to iterate over the enumerable properties of an object.
Published   January 22, 2026
🌐
SitePoint
sitepoint.com › blog › javascript › how to use the for loop in javascript
For Loop in JavaScript: How to Use the for…in Loop — SitePoint
November 7, 2024 - So, if the value variable in the for...in loop syntax structure we showed above was an array of five items, key would not be guaranteed to be 0 to 4. Some indices might precede others. Details on when this might happen is explained later in this article. In the example below, we’re looping over the following variable arr: const arr = ["JavaScript", "PHP", "Python", "Java"]; for (let key in arr) { console.log(key + ": " + arr[key]) } // Output: // "0: JavaScript" // "1: PHP" // "2: Python" // "3: Java"
🌐
Programiz
programiz.com › javascript › for-loop
JavaScript for loop (with Examples)
In JavaScript, the for loop is used for iterating over a block of code a certain number of times or over the elements of an array. In this tutorial, you will learn about the JavaScript for loop with the help of examples.
Find elsewhere
🌐
freeCodeCamp
freecodecamp.org › news › javascript-for-loops
JavaScript For Loop – Explained with Examples
November 7, 2024 - We're only going to look at JavaScript ... The for loop is an iterative statement which you use to check for certain conditions and then repeatedly execute a block of code as long as those conditions are met....
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › loops-in-javascript
JavaScript Loops - GeeksforGeeks
The for-in loop iterates over the enumerable keys of an object. ... Example: The below JavaScript program for-in loop iterates over the keys of the person object and prints each key with its corresponding value.
Published   January 19, 2026
🌐
Mimo
mimo.org › glossary › javascript › for-loops
JavaScript For Loop: Efficient Iteration in JavaScript
Learn HTML, CSS, JavaScript, and React as well as NodeJS, Express, and SQL ... Master the language of the web. Learn variables, functions, objects, and modern ES6+ features ... Classic for loop: Repeats a block of code a specific number of times. It is commonly used to iterate over an array ...
🌐
W3Schools
w3schools.com › js › js_looping.asp
JavaScript Loops
The for...in loop iterates over the enumerable properties of an object. It is typically used for iterating over object keys. for (key in object) { // code block to be executed } A JavaScript for...in statement loops through the properties of ...
🌐
TutorialsPoint
tutorialspoint.com › home › javascript › javascript for...in loop
JavaScript - For...in Loop
September 1, 2008 - The for...in loop in JavaScript is used to loop through an object's properties. The JavaScript for...in loop is a variant of the for loop. The for loop can't be used to traverse through the object properties.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-for-loop
JavaScript For Loop - GeeksforGeeks
JavaScript for loop is a control flow statement that allows code to be executed repeatedly based on a condition. It consists of three parts: initialization, condition, and increment/decrement.
Published   September 27, 2025
🌐
TechOnTheNet
techonthenet.com › js › for_in_loop.php
JavaScript: For-In Loop
This JavaScript tutorial explains how to use the for-in loop with syntax and examples. In JavaScript, the for-in loop is a basic control statement that allows you to loop through the properties of an object.
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_for_loop.htm
JavaScript - For Loop
The syntax of for loop is JavaScript is as follows − · for (initialization; condition; iteration) { Statement(s) to be executed if condition is true }
🌐
Udacity
udacity.com › blog › 2021 › 01 › javascript-for-loop.html
The Many Types of Javascript For Loop | Udacity
September 27, 2022 - The for-of loop returns values, while the for-in loop returns keys or indexes. The for-await-of loop is used when you need to iterate over asynchronous objects or functions. It can return values from objects and the results of function calls.
Top answer
1 of 16
1741

The reason is that one construct:

var a = []; // Create a new empty array.
a[5] = 5;   // Perfectly legal JavaScript that resizes the array.

for (var i = 0; i < a.length; i++) {
    // Iterate over numeric indexes from 0 to 5, as everyone expects.
    console.log(a[i]);
}

/* Will display:
   undefined
   undefined
   undefined
   undefined
   undefined
   5
*/

can sometimes be totally different from the other:

var a = [];
a[5] = 5;
for (var x in a) {
    // Shows only the explicitly set index of "5", and ignores 0-4
    console.log(x);
}

/* Will display:
   5
*/

Also consider that JavaScript libraries might do things like this, which will affect any array you create:

// Somewhere deep in your JavaScript library...
Array.prototype.foo = 1;

// Now you have no idea what the below code will do.
var a = [1, 2, 3, 4, 5];
for (var x in a){
    // Now foo is a part of EVERY array and 
    // will show up here as a value of 'x'.
    console.log(x);
}

/* Will display:
   0
   1
   2
   3
   4
   foo
*/

2 of 16
434

The for-in statement by itself is not a "bad practice", however it can be mis-used, for example, to iterate over arrays or array-like objects.

The purpose of the for-in statement is to enumerate over object properties. This statement will go up in the prototype chain, also enumerating over inherited properties, a thing that sometimes is not desired.

Also, the order of iteration is not guaranteed by the spec., meaning that if you want to "iterate" an array object, with this statement you cannot be sure that the properties (array indexes) will be visited in the numeric order.

For example, in JScript (IE <= 8), the order of enumeration even on Array objects is defined as the properties were created:

var array = [];
array[2] = 'c';
array[1] = 'b';
array[0] = 'a';

for (var p in array) {
  //... p will be "2", "1" and "0" on IE
}

Also, speaking about inherited properties, if you, for example, extend the Array.prototype object (like some libraries as MooTools do), that properties will be also enumerated:

Array.prototype.last = function () { return this[this.length-1]; };

for (var p in []) { // an empty array
  // last will be enumerated
}

As I said before to iterate over arrays or array-like objects, the best thing is to use a sequential loop, such as a plain-old for/while loop.

When you want to enumerate only the own properties of an object (the ones that aren't inherited), you can use the hasOwnProperty method:

for (var prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    // prop is not inherited
  }
}

And some people even recommend calling the method directly from Object.prototype to avoid having problems if somebody adds a property named hasOwnProperty to our object:

for (var prop in obj) {
  if (Object.prototype.hasOwnProperty.call(obj, prop)) {
    // prop is not inherited
  }
}
🌐
W3Schools
w3schools.com › jsref › jsref_forin.asp
JavaScript for...in Loop
The for...in statements combo iterates (loops) over the properties of an object.