The JavaScript for...in loop is used to iterate over the enumerable string-keyed properties of an object. It executes a block of code once for each enumerable property key, including enumerable properties inherited through the object’s prototype chain.
It is especially useful when you need to access the property names of an object.
JavaScript for…in Loop Syntax
The syntax of the for...in loop is:
for (let key in object) {
// Code to execute
}
Explanation:
key: A variable that receives the name of each enumerable property.object: The object whose enumerable properties you want to iterate over.- The loop runs once for each enumerable string-keyed property.
JavaScript for…in Loop Example
Let’s look at a simple example:
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
for (let key in person) {
console.log(key);
}
Output:
firstName
lastName
age
In this example, the for...in loop iterates over the person object’s enumerable properties and prints each property name.
Access Object Values Using for…in
You can use the current property key to access its corresponding value with bracket notation.
const person = {
firstName: "John",
lastName: "Doe",
age: 30
};
for (let key in person) {
console.log(key + ": " + person[key]);
}
Output:
firstName: John
lastName: Doe
age: 30
Here, person[key] retrieves the value associated with the current property name.
JavaScript for…in Loop with Object.hasOwn()
The for...in loop can also visit enumerable properties inherited from an object’s prototype. If you only want an object’s own properties, use Object.hasOwn().
const person = {
name: "John",
age: 30
};
const employee = Object.create(person);
employee.job = "Developer";
for (let key in employee) {
if (Object.hasOwn(employee, key)) {
console.log(key + ": " + employee[key]);
}
}
Output:
job: Developer
The name and age properties are inherited from person, so Object.hasOwn() excludes them.
Iterate Over Inherited Properties
By default, for...in includes enumerable properties inherited through the prototype chain.
const parent = {
country: "Bangladesh"
};
const student = Object.create(parent);
student.name = "Rahim";
student.age = 22;
for (let key in student) {
console.log(key);
}
Output:
name
age
country
The country property is inherited from parent, but it is still enumerable and therefore appears in the loop.
JavaScript for…in Loop with Arrays
Although you can use for...in with arrays, it iterates over enumerable property keys rather than directly iterating over array values.
const fruits = ["Apple", "Banana", "Mango"];
for (let index in fruits) {
console.log(index, fruits[index]);
}
Output:
0 Apple
1 Banana
2 Mango
For arrays, for...of is generally a better choice when you want to access values directly.
const fruits = ["Apple", "Banana", "Mango"];
for (let fruit of fruits) {
console.log(fruit);
}
Output:
Apple
Banana
Mango
for…in vs for…of in JavaScript
| Feature | for…in | for…of |
|---|---|---|
| Iterates over | Enumerable property keys | Iterable values |
| Common use | Objects | Arrays, strings, Maps, Sets |
| Inherited enumerable properties | Included | Not applicable in the same way |
| Returns | Property key | Value |
Example:
const colors = ["Red", "Green", "Blue"];
for (let index in colors) {
console.log(index);
}
for (let color of colors) {
console.log(color);
}
Output:
0
1
2
Red
Green
Blue
Important Things to Remember
- The
for...inloop iterates over enumerable string-keyed properties. - It includes enumerable inherited properties.
- Symbol-keyed properties are not included.
- Use
Object.hasOwn()to check whether a property belongs directly to an object. - Use
for...ofwhen you want to iterate over array values. - Avoid relying on
for...infor array iteration. - Avoid adding, deleting, or modifying properties during iteration when predictable results are important
Check Enumerable Properties with for…in
You can use the for...in loop with Object.hasOwn() to distinguish an object’s own properties from inherited properties. You can also use Object.keys() to get an array containing an object’s own enumerable property names.
const user = {
name: "Rahim",
age: 25
};
const keys = Object.keys(user);
for (let key of keys) {
console.log(key, user[key]);
}
Output:
name Rahim
age 25
This approach is useful when you want to work only with an object’s own enumerable properties without including inherited properties.
When Should You Use the for…in Loop?
Use the JavaScript for...in loop when you need to inspect or process the property names of an object. For example, it can help display object data, validate property values, or create a list of available object properties.
However, remember that it also visits enumerable inherited properties. If you only need an object’s own properties, use Object.hasOwn() inside the loop or choose Object.keys() when an array of property names is more convenient.