JavaScript Own, Inherited and Enumerable Properties
Quick answer
Ownership and enumerability are separate property characteristics. An inherited property can be enumerable, and an own property can be hidden from ordinary enumeration.
How it works
Object.hasOwn(o, k) checks own presence; k in o also checks prototypes. Object.keys returns only enumerable own string keys. A for...in loop can include enumerable inherited string keys. Object.prototype.propertyIsEnumerable checks own enumerability; defineProperty can create non-enumerable properties, as shown here and explained in detail shortly.
const o = Object.create({inherited: 1}); o.own = 2;console.log("inherited" in o, Object.hasOwn(o,"inherited"), Object.keys(o).join(","));Expected output in Node.js:
true false ownPresence, ownership and enumeration answer different questions.
const o = {}; Object.defineProperty(o,"hidden",{value: 5});console.log(o.hidden, Object.hasOwn(o,"hidden"), o.propertyIsEnumerable("hidden"));Expected output in Node.js:
5 true falseThe own property is readable but not enumerable.
Common mistakes
Non-enumerable does not mean private or inaccessible.
Practice
Compare direct access, Object.keys and in on the hidden field.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Own, Inherited and Enumerable Properties — Knowledge Check
Apply the taught concept and predict the output.
Which check includes inherited property presence?
Can an own non-enumerable property be read directly?
Does Object.keys include symbol keys?
What is the exact console output in JavaScript Own, Inherited and Enumerable Properties example 1?
const o = Object.create({inherited: 1}); o.own = 2;console.log("inherited" in o, Object.hasOwn(o,"inherited"), Object.keys(o).join(","));What is the exact console output in JavaScript Own, Inherited and Enumerable Properties example 2?
const o = {}; Object.defineProperty(o,"hidden",{value: 5});console.log(o.hidden, Object.hasOwn(o,"hidden"), o.propertyIsEnumerable("hidden"));