Technical skill practice

JavaScript for...in: Enumerate Properties and Guard Own Keys

for...in iterates enumerable string property keys, including inherited ones. It does not mean “visit array values.” Add an own-property guard when the operation should apply only to directly owned fields.

The task

Inspect a small status record with an inherited category and a hidden note. Collect keys first, then collect only own keys using a guard.

Runnable Node 22 example

const assert = require('node:assert/strict');

const report = Object.create({category: 'inventory'});
report.open = 2;
Object.defineProperty(report, 'privateNote', {value: 'draft', enumerable: false});
const all = [];
const own = [];
for (const key in report) {
  all.push(key);
  if (Object.hasOwn(report, key)) own.push(key);
}
assert.deepEqual(all.sort(), ['category', 'open']);
assert.deepEqual(own, ['open']);
const values = [];
for (const value of ['east', 'west']) values.push(value);
assert.deepEqual(values, ['east', 'west']);
console.log(`all=${all.join(',')}; own=${own.join(',')}`);

Expected output and explanation

The inherited category appears in the unguarded key list, while the non-enumerable privateNote does not. The own-property guard retains only open. The sort is used to make the diagnostic comparison independent of the collection order this task does not need.

The separate for...of loop demonstrates value iteration on an array. Using for...in there would produce keys rather than the east and west strings. Choose the loop from the structure and the values the body actually needs.

Output from the example

all=category,open; own=open

Pitfall to check

Do not mutate a prototype chain during enumeration and assume a simple static key list. This fixture keeps the object stable. If a fixed own-key snapshot is the requirement, Object.keys followed by a loop may express it more directly.

Questions, answered

Does for...in include symbol keys?

No. Its enumerable-key behavior concerns string keys. Use a suitable own-key operation when symbols are part of the required inspection.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.