Middle
What is the difference between Object.getOwnPropertyNames() and Object.keys() methods?
sobes.tech AI
Answer from AI
Object.keys() returns an array of strings - the names of the enumerable properties of the object. Object.getOwnPropertyNames() returns an array of strings - the names of all properties of the object (including non-enumerable ones), except for symbol properties.
Example:
const obj = {
a: 1,
b: 2
};
Object.defineProperty(obj, 'c', {
value: 3,
enumerable: false
});
console.log(Object.keys(obj)); // Output: [ 'a', 'b' ]
console.log(Object.getOwnPropertyNames(obj)); // Output: [ 'a', 'b', 'c' ]