-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
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 Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Window
JS Screen
JS Location
JS History
JS Navigator
JS Popup Alert
JS Timing
JS Cookies
Web Storage API
JS Web APIs
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Canvas
JS Graphics & Charts
for...in
Loop – Looping Over Object PropertiesThe for...in
loop is used to iterate over enumerable properties (keys) of an object.
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.
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
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, preferfor
,forEach
, orfor...of
.
To avoid inherited properties:
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
// process own property
}
}
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.
Q1: What does the for...in loop iterate over?
Q2: Which is the correct syntax of a for...in loop?
Q3: What is the value of key inside a for...in loop?
Q4: Which loop is NOT suitable for arrays?
Q5: What function can be used inside a for...in loop to check if a property belongs to the object directly?
Q6: How can you prevent inherited properties from being accessed in a for...in loop?
Q7: What happens if you use for...in on an empty object?