Unlock the Power of ‘For…In’ Loop in JavaScript: A Complete Guide

Dive deeper into understanding the basics, advantages, disadvantages, and practical uses of JavaScript’s ‘For…In’ loop with real-world examples

Max N
3 min readMar 17, 2024

JavaScript offers various ways to iterate over collections and objects, such as for, while, do...while, and the lesser-known but equally important for...in. This comprehensive guide explores the ins and outs of the for...in loop in JavaScript, providing clear explanations and relevant code snippets along the way.

What Is a ‘For…In’ Loop?

The for...in loop is designed specifically for iterating over properties of an object. Unlike traditional for loops which rely on numerical indices, the for...in statement enumerates keys found within an object, making it particularly useful when dealing with dynamic property names or unknown object structures.

Basic Syntax & Example

Let’s consider a basic example demonstrating its syntax and usage:

const person = {
name: "John Doe",
age: 35,
occupation: "Software Engineer",
};

for (const key in person) {
console.log(`${key}: ${person[key]}`);
}

Output:

--

--

Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.