Developers search the same task many ways: javascript foreach object, js foreach object, foreach object js, or object foreach javascript. Natural-language variants like javascript loop through object, loop through object javascript, and js loop through object are the same operation. Shorter js object foreach and js iterate object / javascript iterate object queries still mean “visit each key (and maybe value) on an object.”
There is no Object.prototype.forEach—you combine Array.prototype.forEach with Object.keys or Object.entries, or you use for...in / for...of. For the mental model of forEach versus for...in, see JavaScript forEach vs forIn.
Environment: Node.js v20.18.2. Snippets use only standard library APIs; each block’s note describes the expected console output.
Sample object (null prototype)
The demos use a null-prototype object so a for...in loop does not pick up inherited enumerable keys from Object.prototype. For ordinary {} literals, guard with Object.hasOwn (or Object.prototype.hasOwnProperty.call(obj, key)) when you want to skip inherited properties.
const base = Object.assign(Object.create(null), {
complexity: 2,
condition: true,
blog: "GoLinux",
tag: "js",
});for...in (enumerable keys, string names)
for...in walks enumerable property names, including inherited ones on normal objects—hence the guard advice above.
for (const k in base) {
console.log(`${k}: ${base[k]}`);
}You should see 4 lines, in order: complexity: 2, condition: true, blog: GoLinux, tag: js.
Object.keys + forEach (own enumerable string keys)
Object.keys materializes own enumerable string property names, then forEach is the javascript foreach object pattern people expect.
Object.keys(base).forEach((prop) => {
console.log(`${prop}: ${base[prop]}`);
});You should see 4 lines, in order: complexity: 2, condition: true, blog: GoLinux, tag: js.
Object.entries + forEach (own enumerable key/value pairs)
Object.entries yields [key, value] arrays—handy when you need both without extra lookups.
Object.entries(base).forEach(([prop, value]) => {
console.log(`${prop}: ${value}`);
});You should see 4 lines, in order: complexity: 2, condition: true, blog: GoLinux, tag: js.
for...of with Object.entries
Same data as the forEach variant, often clearer when you want break / continue.
for (const [prop, value] of Object.entries(base)) {
console.log(`${prop}: ${value}`);
}You should see 4 lines, in order: complexity: 2, condition: true, blog: GoLinux, tag: js.
Object.getOwnPropertyNames (own string keys, enumerable or not)
Object.getOwnPropertyNames lists every own string key, even non-enumerable ones (not shown in the tiny sample below, but important for descriptors). It does not walk the prototype chain—only own properties of the target object.
const person = {
name: "John",
age: 30,
getFullName() {
return `${this.name} Doe`;
},
};
Object.getOwnPropertyNames(person).forEach((propertyName) => {
const v = person[propertyName];
const kind = typeof v === "function" ? "function" : String(v);
console.log(`${propertyName}: ${kind}`);
});You should see 3 lines, in order: name: John, age: 30, getFullName: function.
Object.getOwnPropertySymbols (own symbol keys)
Object.getOwnPropertySymbols returns symbol keys that are own properties—useful alongside string keys when modeling metadata.
const age = Symbol("age");
const person = {
name: "John",
[age]: 30,
};
Object.getOwnPropertySymbols(person).forEach((symbol) => {
console.log(`${symbol.toString()}: ${person[symbol]}`);
});You should see one line logging Symbol(age): 30.
Reflect.ownKeys (own strings + symbols)
Reflect.ownKeys returns the full own-key list (strings and symbols) in a single array. Like getOwnPropertyNames / getOwnPropertySymbols, it is about own keys, not inherited prototype properties.
const age = Symbol("age");
const person = {
name: "John",
[age]: 30,
};
Reflect.ownKeys(person).forEach((key) => {
console.log(`${String(key)}: ${person[key]}`);
});You should see 2 lines, in order: name: John, Symbol(age): 30.
Summary
Searches such as javascript foreach object, js loop through object, javascript iterate object, or foreach object js all describe the same gap: JavaScript objects are not arrays, so there is no obj.forEach. The mainstream answers are Object.keys / Object.entries paired with Array.prototype.forEach, a for...of loop over Object.entries, or for...in when you accept enumerable string keys and remember inherited properties on ordinary {} literals (where Object.hasOwn guards are the usual FAQ follow-up).
When questions move to symbols or non-enumerable fields, Object.getOwnPropertyNames, Object.getOwnPropertySymbols, and Reflect.ownKeys enumerate own keys only—not the prototype chain—so choose the helper that matches whether you need strings, symbols, or both. If you are debugging, combine any of these patterns with how to print an object so you can see shapes clearly without mutating production data.
