JavaScript Object.keys() is a built-in static method that returns an array of a given object’s enumerable property names, in the same order as that provided by a for…in loop.
The keys() method only includes properties directly defined on the object, not those inherited from its prototype chain.
let obj = { name: 'Krunal', education: 'IT Engineer', country: 'India' }; console.log(Object.keys(obj)); // Output: ['name', 'education', 'country']
You can see that the output array contains the keys as elements.
Syntax
Object.keys(obj)
Parameters
Argument | Description |
obj | It represents an input object whose enumerable properties you want. |
Iterating over properties
At first glance, you can assume that it only returns keys, but you can iterate over an object and print its keys and values with the help of the forEach() function.
let student = { name: 'Krunal', education: 'IT Engineer', country: 'India' }; Object.keys(student).forEach(key => { console.log(`${key}: ${student[key]}`); }); // Output: // name: Krunal // education: IT Engineer // country: India
We displayed Object keys and values as text in the console. You can further process these values based on your requirements. It enables access to Object values.
Working with an Array
In JavaScript, arrays are Objects. So, we can apply the Object.keys() method to arrays as well.
Since an array does not have a key like an object does, its indices can be counted as keys.
It will return indices as strings of an array.
// Declaring an array let arr = [ 'apple', 'microsoft', 'amazon', 'alphabet', 'tencent', ]; console.log(Object.keys(arr)); // Output: [ '0', '1', '2', '3', '4' ]
Non-Enumerable properties
When creating a new object with properties defined as enumerable: false, the Object.keys() method excludes those properties from the output.
const obj = {}; Object.defineProperty(obj, "hidden", { value: "secret", enumerable: false }); obj.visible = "public"; console.log(Object.keys(obj)); // Output: ["visible"]
In the above code, the property hidden does not appear in the final keys array because we set the enumerable to false. The output array only contains one property, “visible”.
Inherited properties
Inherited properties from the prototype chain are not included in the output keys array.
function Student() { this.name = "Charlie"; } Student.prototype.age = 21; const student = new Student(); console.log(Object.keys(student)); // Output: ['name']
String Object
Strings are iterables and are coerced to String objects, treating each character as a property. That means, it can return the indices of characters as keys.
console.log(Object.keys("hello")); // Output: [ '0', '1', '2', '3', '4' ]
Empty Objects
When the input Object is empty, the output array won’t include a single element. No enumerable properties result in an empty array.
console.log(Object.keys({})); // Output: []
Proxy Objects
This method works with proxies, but the behavior depends on the proxy’s trap implementation.
const obj = { kb: 19, kl: 21 }; const proxy = new Proxy(obj, { ownKeys() { return ["kb"]; } }); console.log(Object.keys(proxy)); // Output: ["kb"]
In the above output, the proxy’s ownKeys trap can override the default behavior, affecting the returned keys.
That’s it!