DEV Community

Max
Max

Posted on

3

JavaScript for in loop

In JavaScript, for...in loop is similar to for loop to iterate over an objects or array elements. The loop allows you to perform an action on each property of an object or element of an array.

Syntax of the Javascript For In Loop

for(var property in object)  
{  
    // code to be executed  
}
Enter fullscreen mode Exit fullscreen mode

Here, property is a string variable representing the property key, and object is the object to be iterated over.

Iterating Over an Object Example

const numbers = {
    "one": 1,
    "two": 2,
    "three": 3,
    "four": 4,
    "five": 5
}

for (let num in numbers) {
  console.log(`${num}: ${numbers[num]}`);
}
Enter fullscreen mode Exit fullscreen mode

Output:

one: 1
two: 2
three: 3
four: 4
five: 5
Enter fullscreen mode Exit fullscreen mode

Iterating Over an Array Example

const numbers = [1, 2, 3, 4, 5]; 
for (let index in numbers) {
    console.log(`Index: ${index} Value: ${numbers[index]}`); 
}
Enter fullscreen mode Exit fullscreen mode

Output:

Index: 0 Value: 1
Index: 1 Value: 2
Index: 2 Value: 3
Index: 3 Value: 4
Index: 4 Value: 5
Enter fullscreen mode Exit fullscreen mode

Learn Javascript Tutorial

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs