Object.getOwnPropertyNames() Includes Non-Enumerable
You'll learn how Object.getOwnPropertyNames() reveals all own string properties, even the hidden non-enumerable ones that other iteration methods skip.
The Hidden Properties Problem
You already know that methods like Object.keys() and for...in loops only show enumerable properties. But sometimes objects have properties that are intentionally hidden from normal iteration—these are non-enumerable properties.
Think of an object like a house: enumerable properties are the rooms everyone can see on the tour, while non-enumerable properties are the maintenance closets and utility rooms—still part of the house, but not shown to casual visitors.
What Object.getOwnPropertyNames() Does
Object.getOwnPropertyNames() is like having the full blueprint of the house. It returns an array of all own string-keyed property names, regardless of their enumerability:
const obj = {};
// Add an enumerable property
obj.visible = "I'm enumerable";
// Add a non-enumerable property
Object.defineProperty(obj, 'hidden', {
value: "I'm non-enumerable",
enumerable: false
});
console.log(Object.keys(obj));
// ['visible'] - only enumerable
console.log(Object.getOwnPropertyNames(obj));
// ['visible', 'hidden'] - everything!
This is particularly useful for debugging or when you need to inspect every property an object actually has, not just the ones meant for public consumption.
Key Takeaway: Object.getOwnPropertyNames() returns all own string property names including non-enumerable ones, making it more comprehensive than Object.keys() which only returns enumerable properties.