Object.getOwnPropertyNames(obj)
returns all the properties of the object obj
. Object.keys(obj)
returns all enumerable properties.enumerable: false
to any property.screen
object has two properties, branch
and size
.const screen = {
branch: 'Dell',
size: '27inch',
};
Object.getOwnPropertyNames(screen); // ['branch', 'size']
Object.keys(screen); // ['branch', 'size']
resolution
but it is set as enumerable: false
:Object.defineProperties(screen, {
resolution: {
enumerable: false,
value: '2560 x 1440',
},
});
resolution
property then doesn't appear in the list of Object.keys
:Object.getOwnPropertyNames(screen); // ['branch', 'size', 'resolution']
Object.keys(screen); // ['branch', 'size']
Object.getOwnPropertyNames
includes an extra property named length
which is the size of the array.const animals = ['dog', 'cat', 'tiger'];
Object.keys(animals); // ['0', '1', '2']
Object.getOwnPropertyNames(animals); // ['0', '1', '2', 'length']