Question
In JavaScript, consider this object and loop:
var obj = {
name: "Simon",
age: "20",
clothing: {
style: "simple",
hipster: false
}
};
for (var propt in obj) {
console.log(propt + ': ' + obj[propt]);
}
How does the variable propt represent the properties of the object? It is not a built-in method or property. Why does it receive each property name from the object during the loop?
Short Answer
By the end of this page, you will understand how the JavaScript for...in loop works, why the loop variable can have any name, and how it gets assigned each enumerable property name of an object one at a time. You will also learn safer modern alternatives such as Object.keys(), Object.values(), and Object.entries().
Concept
In JavaScript, for...in is a special loop used to iterate over the property names of an object.
When you write:
for (var propt in obj) {
console.log(propt);
}
JavaScript reads this as:
- Look at the object
obj - Find its enumerable property names
- For each property name, assign that name to the variable
propt - Run the loop body
The important point is that propt is just a variable name you chose. It is not built in. You could call it key, property, x, or anything valid:
for (var key in obj) {
console.log(key);
}
This works the same way.
In your example, the loop visits these top-level properties:
name
Mental Model
Think of an object like a labeled storage cabinet:
nameis one drawer labelageis another drawer labelclothingis another drawer label
A for...in loop is like saying:
"Go through each drawer label in this cabinet, one by one, and write the current label on a sticky note."
That sticky note is your loop variable.
So propt is not special. It is just the sticky note name you chose. JavaScript keeps updating it with the next property label during the loop.
Syntax and Examples
Basic syntax
for (var key in object) {
// use key
}
keyis a normal variableobjectis the object being inspected- On each loop,
keycontains the next property name as a string
Example: logging keys and values
var user = {
name: "Simon",
age: 20,
active: true
};
for (var key in user) {
console.log(key); // property name
console.log(user[key]); // property value
}
Possible output:
name
Simon
age
20
active
true
Why bracket notation is used
This works:
Step by Step Execution
Consider this code:
var obj = {
name: "Simon",
age: 20,
clothing: {
style: "simple",
hipster: false
}
};
for (var propt in obj) {
console.log(propt + ': ' + obj[propt]);
}
Step by step:
-
JavaScript creates the object
objwith three properties:nameageclothing
-
The loop starts:
for (var propt in obj)JavaScript prepares to iterate through the enumerable property names of
obj. -
First iteration:
proptis assigned
Real World Use Cases
for...in and object key iteration are useful when property names are not known in advance.
Common situations
- Form validation: loop through submitted fields and check for empty values
- Configuration objects: inspect settings and apply defaults
- API response formatting: transform object fields into display text
- Debugging: log all properties in an object
- Data cleanup: remove or normalize invalid values
Example: validating form data
var formData = {
username: "simon",
email: "",
password: "secret123"
};
for (var key in formData) {
if (formData[key] === "") {
console.log(key + " is required");
}
}
Example: printing config values
var config = {
theme: "dark",
notifications: true,
language: "en"
};
for (var key config) {
.(key + + config[key]);
}
Real Codebase Usage
In real projects, developers often iterate over object properties to process dynamic data. However, they usually do it carefully.
Common patterns
1. Filtering own properties
Because for...in can include inherited enumerable properties, developers often guard against that:
for (var key in obj) {
if (Object.hasOwn(obj, key)) {
console.log(key, obj[key]);
}
}
2. Using Object.keys() for safer iteration
Object.keys(obj).forEach(function (key) {
console.log(key, obj[key]);
});
This only returns the object's own enumerable property names.
3. Using Object.entries() when both key and value are needed
Object.entries(obj).forEach( () {
.(key, value);
});
Common Mistakes
1. Thinking the loop variable is built in
Wrong idea:
propthas special meaning
Reality:
- It is just a variable name you chose
These are all valid:
for (var propt in obj) {}
for (var key in obj) {}
for (var propertyName in obj) {}
2. Using dot notation with the variable name
Broken code:
for (var key in obj) {
console.log(obj.key);
}
Why it is wrong:
obj.keymeans a property literally calledkey- It does not mean "use the value inside the variable
key"
Correct code:
( key obj) {
.(obj[key]);
}
Comparisons
for...in vs other ways to iterate objects
| Approach | What it iterates | Best use | Notes |
|---|---|---|---|
for...in | Enumerable property names | Quick object key iteration | Can include inherited properties |
Object.keys(obj) | Own enumerable property names | Safe key iteration | Returns an array of keys |
Object.values(obj) | Own enumerable values | When only values matter | No keys returned |
Object.entries(obj) | Own enumerable key-value pairs | When you need both key and value | Very readable |
Example comparison
Cheat Sheet
Quick reference
for...in syntax
for (var key in obj) {
console.log(key, obj[key]);
}
Key facts
- The loop variable can have any valid name
- It stores the current property name as a string
- Use bracket notation:
obj[key] for...initerates enumerable properties- It may include inherited properties
Safer pattern
for (var key in obj) {
if (Object.hasOwn(obj, key)) {
console.log(key, obj[key]);
}
}
Modern alternatives
Object.keys(obj)
Object.values(obj)
Object.entries(obj)
Common pitfall
FAQ
Why can I name the loop variable anything in for...in?
Because it is just a normal variable. JavaScript assigns each property name to that variable during the loop.
Does for...in return values or keys?
It returns property names, also called keys. You then use obj[key] to get the value.
Why do I need obj[key] instead of obj.key?
obj[key] uses the variable's current value. obj.key looks for a property literally named key.
Does for...in work on nested objects automatically?
No. It only iterates the current object's top-level enumerable properties.
Is for...in safe for all objects?
It works, but it can include inherited properties. Use Object.hasOwn() or prefer Object.keys() when you only want the object's own keys.
Should I use for...in for arrays?
Usually no. Arrays are better handled with for, , or .
Mini Project
Description
Create a small utility that prints a user profile object in a readable way. This project helps you practice iterating through object properties, reading keys and values dynamically, and skipping nested objects when needed.
Goal
Build a function that loops through an object and prints each top-level property name with its value.
Requirements
[ "Create an object with at least four properties", "Use a for...in loop to iterate through the object", "Print each property name and its value", "If a value is an object, print a placeholder message instead of the full nested object" ]
Keep learning
Related questions
Deep Cloning Objects in JavaScript: Methods, Trade-offs, and Best Practices
Learn how to deep clone objects in JavaScript, compare structuredClone, JSON methods, and recursive approaches with examples.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers with clear examples.
How JavaScript Closures Work: A Beginner-Friendly Guide
Learn how JavaScript closures work with simple explanations, examples, common mistakes, and practical use cases for real code.