Introduction
for…in loop, which is specifically designed to iterate over the properties (keys) of an object.
Understanding the for…in loop is essential when working with objects, APIs, JSON data, and dynamic structures in JavaScript.
In this guide, you’ll learn everything about the JavaScript for…in loop, including syntax, usage, real-life examples, common mistakes, and interview questions.
What is a for…in Loop?
The for…in loop is used to iterate over the enumerable properties (keys) of an object.
Note: It gives you access to each key in the object.
- Use for…in for objects only.
- Avoid using with arrays.
- Use hasOwnProperty().
Syntax of for…in Loop
for (let key in object) {
// code
}
Basic Example: Access Key from the Object
let user = {
name: "John",
age: 35,
city: "Delhi"
};
for (let key in user) {
console.log(key);
}
Output:
age
city
Access Value from the Object
let user = {
name: "John",
age: 35,
city: "Delhi"
};
for (let key in user) {
console.log(user[key]);
}
Output:
35
Delhi
How for…in Loop Works
There are 4 steps
- Picks each key from the object
- Assigns it to a variable (key)
- Executes a block of code
- Continues until all keys are covered
Real-Life Examples
You will see some Real-Life Examples
1. User Profile
let profile = {
name: "John",
age: 35,
profession: "Manager"
};
for (let key in profile) {
console.log(`${key}: ${profile[key]}`);
}
2. API Data Handling
let data = {
id: 1,
title: "Post",
status: "published"
};
for (let key in data) {
console.log(key, data[key]);
}
for…in with Nested Objects
let user = {
name: "John",
address: {
city: "Delhi",
zip: 12345
}
};
for (let key in user) {
if (typeof user[key] === "object") {
for (let innerKey in user[key]) {
console.log(innerKey, user[key][innerKey]);
}
} else {
console.log(key, user[key]);
}
}
Output:
city Delhi
zip 12345
Difference between for…in vs for…of
| Feature | for…in | for…of |
|---|---|---|
| Iterates | Keys / Indexes | Values |
| Used For | Objects | Arrays / Iterables |
| Returns | Property names | Element values |
| Example Syntax |
for(let key in obj){
console.log(key);
}
|
for(let value of arr){
console.log(value);
}
|
| Example Output | name, age | John, 25 |
When to Use for…in Loop
There are many reasons to use for…in a loop
- Working with objects
- Iterating keys
- Handling JSON data
- Dynamic property access
When NOT to Use for…in Loop
You should avoid when
- Working with arrays
- Needing values directly
- Performance-critical loops
JavaScript for in loop – Interview Questions
Q 1: What is for...in a loop?
Q 2: Returns key or value?
Q 3: Is it suitable for arrays?
Q 4: Does it include inherited properties?
Q 5: Use case?
JavaScript for in loop – Objective Questions (MCQs)
Q1. for...in is used to iterate over ______.
Q2. for...in is best suited for ______.
Q3. for...in iterates over ______ properties.
Q4. Which loop should NOT be preferred for arrays?
Q5. for...in also iterates over ______ properties.
Conclusion
The JavaScript for…in loop is a powerful tool for iterating over object properties. It is especially useful when working with dynamic data structures, APIs, and JSON objects.
However, it should be used carefully, especially when dealing with arrays or inherited properties.
By understanding how and when to use the for…in loop, you can write more efficient and maintainable JavaScript code.