7 JavaScript Object Traversal Techniques Illustrated with Allen Iverson

This article examines seven ways to iterate over JavaScript objects—excluding arrays, Map, and Set—using a sample player object modeled after Allen Iverson, detailing each method’s handling of own, inherited, enumerable, non‑enumerable, and Symbol properties, with code examples and output analysis.

WeDoctor Frontend Technology
WeDoctor Frontend Technology
WeDoctor Frontend Technology
7 JavaScript Object Traversal Techniques Illustrated with Allen Iverson

Introduction

This article summarizes seven traversal methods for plain JavaScript objects (excluding arrays, Map, and Set). The examples use a player object that mimics the basketball star Allen Iverson, allowing us to observe how each method treats own, inherited, enumerable, non‑enumerable, and Symbol properties.

Define the Object

const player = {
    name: 'Allen  Iverson',
    [Symbol('birthday')]: '1975/06/07',
};
Object.defineProperties(player, {
    isHallofFame: { enumerable: false, value: true },
    [Symbol('ScoreKingTime')]: { enumerable: false, value: 4 },
});
Object.defineProperties(player.__proto__, {
    university: { enumerable: true, value: 'Georgetown' },
    team: { enumerable: false, value: '76ers' },
    [Symbol('country')]: { enumerable: true, value: 'USA' },
    [Symbol('hometown')]: { enumerable: false, value: 'Virginia' },
});

The object has eight properties, a mix of string‑keyed and Symbol keys, some enumerable and some not, plus prototype properties.

Console output showing enumerable and non‑enumerable properties
Console output showing enumerable and non‑enumerable properties

for...in

MDN: The for...in statement iterates over all enumerable properties of an object that are keyed by strings (ignoring ones keyed by Symbols), including inherited enumerable properties.
for (const name in player) {
    console.log('name:', name);
}
// name: name
// name: university

The loop enumerates own and prototype enumerable string‑keyed properties but skips Symbol keys. A common guard is Object.prototype.hasOwnProperty.call(player, name) to exclude prototype properties.

Object.keys

MDN: Object.keys() returns an array of a given object's own enumerable property names , iterated in the same order as a normal loop would.
const keys = Object.keys(player);
console.log('keys:', keys);
// keys: ["name"]
Object.keys

returns only the object's own enumerable string‑keyed properties (no Symbol, no prototype properties).

Object.getOwnPropertyNames

MDN: Object.getOwnPropertyNames() returns an array of all properties (including non‑enumerable properties except those using Symbol ) found directly in a given object.
const ownPropertyNames = Object.getOwnPropertyNames(player);
console.log('ownPropertyNames:', ownPropertyNames);
// ownPropertyNames: ["name", "isHallofFame"]

This method returns all own properties, enumerable or not, but excludes Symbol keys.

Object.getOwnPropertySymbols

MDN: Object.getOwnPropertySymbols() returns an array of all Symbol properties found directly upon a given object.
const ownPropertySymbols = Object.getOwnPropertySymbols(player);
console.log('ownPropertySymbols:', ownPropertySymbols);
// ownPropertySymbols: [Symbol(birthday), Symbol(ScoreKingTime)]

It returns every own Symbol property, regardless of enumerability, and does not include prototype symbols.

Reflect.ownKeys

MDN: The static Reflect.ownKeys() method returns an array of the target object's own property keys.
const ownKeys = Reflect.ownKeys(player);
console.log('ownKeys:', ownKeys);
// ownKeys: ["name", "isHallofFame", Symbol(birthday), Symbol(ScoreKingTime)]
Reflect.ownKeys

returns all own property keys, both string‑keyed and Symbol, enumerable or not.

Object.values

MDN: Object.values() returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (which also includes prototype properties).
const values = Object.values(player);
console.log('values:', values);
// values: ["Allen  Iverson"]

Like Object.keys, it only includes own enumerable string‑keyed properties, but returns the values instead of the keys.

Object.entries

MDN: The Object.entries() method returns an array of a given object's own enumerable string‑keyed [key, value] pairs, in the same order as a for...in loop (excluding prototype properties).
const entries = Object.entries(player);
console.log('entries:', entries);
// entries: [["name", "Allen  Iverson"]]

It behaves like Object.keys but returns both keys and values, useful together with Object.fromEntries for transformations.

Summary Table

for...in : includes prototype enumerable properties; excludes non‑enumerable and Symbol keys.

Object.keys : own enumerable string‑keyed properties only.

Object.getOwnPropertyNames : own properties, including non‑enumerable, but excludes Symbol keys.

Object.getOwnPropertySymbols : own Symbol properties (including non‑enumerable), no prototype symbols.

Reflect.ownKeys : all own property keys (string‑keyed and Symbol, enumerable or not).

Object.values : own enumerable string‑keyed property values only.

Object.entries : own enumerable string‑keyed [key, value] pairs.

Conclusions

Only for...in can traverse prototype properties. Object.getOwnPropertyNames and Reflect.ownKeys can retrieve non‑enumerable properties. Object.getOwnPropertySymbols and Reflect.ownKeys can retrieve Symbol properties.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

SymbolreflectObject.keysfor...inObject Traversalenumerable properties
WeDoctor Frontend Technology
Written by

WeDoctor Frontend Technology

Official WeDoctor Group frontend public account, sharing original tech articles, events, job postings, and occasional daily updates from our tech team.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.