JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JavaScript

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Loop For In


📘 JavaScript for...in Loop – Looping Over Object Properties

The for...in loop is used to iterate over enumerable properties (keys) of an object.


🔹 Syntax

for (let key in object) {
  // code block to be executed
}
  • key is a variable that will hold the property name (key) in each iteration.

  • object is the object you're looping through.


🔹 Example 1 – Looping Through an Object

let person = {
  name: "Alice",
  age: 25,
  city: "New York"
};

for (let key in person) {
  console.log(key + ": " + person[key]);
}

📤 Output:

name: Alice  
age: 25  
city: New York

🔹 Example 2 – Looping Through an Array (Not Recommended)

let colors = ["red", "green", "blue"];

for (let index in colors) {
  console.log(index + ": " + colors[index]);
}

📤 Output:

0: red  
1: green  
2: blue

⚠️ Use for...in with objects, not arrays. For arrays, prefer for, forEach, or for...of.


🔹 Checking for Own Properties

To avoid inherited properties:

for (let key in obj) {
  if (obj.hasOwnProperty(key)) {
    // process own property
  }
}

Practice Questions

Q1. How do you loop through all properties of a JavaScript object using for...in?

Q2. What variable does the for...in loop assign in each iteration?

Q3. How can you print both the key and the value in a for...in loop?

Q4. What is the output of looping through an object with 3 properties using for...in?

Q5. Is it recommended to use for...in for arrays? Why or why not?

Q6. How do you check if a property belongs directly to an object inside a for...in loop?

Q7. Can for...in loop iterate over inherited properties?

Q8. How do you loop through a nested object using for...in?

Q9. How can you collect all keys of an object into an array using for...in?

Q10. Write a for...in loop that sums all numeric values in an object.


JS Loop For In Quiz

Q1: What does the for...in loop iterate over?

A. Array values
B. Object keys
C. Function parameters
D. Characters in a string

Q2: Which is the correct syntax of a for...in loop?

A. for key in object {}
B. for (key in object)
C. foreach (object in key)
D. for in (object)

Q3: What is the value of key inside a for...in loop?

A. Object
B. Index
C. Property name
D. Property value

Q4: Which loop is NOT suitable for arrays?

A. for
B. forEach
C. for...of
D. for...in

Q5: What function can be used inside a for...in loop to check if a property belongs to the object directly?

A. Object.has()
B. hasOwnProperty()
C. hasKey()
D. propertyCheck()

Q6: How can you prevent inherited properties from being accessed in a for...in loop?

A. if (obj.hasKey(k))
B. if (k in obj)
C. if (obj.hasOwnProperty(k))
D. if (obj.keyExists(k))

Q7: What happens if you use for...in on an empty object?

A. Error
B. Loops once
C. No output
D. Undefined

Go Back Top