.keys()
CliffordMapesa5 total contributions
Published Dec 27, 2023
Contribute to Docs
The .keys()
static method extracts keys from an object and returns them as an array. It only returns the keys for the object’s property and only for enumerable properties.
Note: The order of the keys in the resulting array is not guaranteed to be the same as the order in which they were defined in the object.
Syntax
The basic syntax is:
Object.keys(obj)
obj
: An object from which the keys are extracted.
Example
Here’s a simple example that extracts keys from an object and returns them as an array:
const myObject = {name: 'John',age: 25,city: 'London',};const keysArray = Object.keys(myObject);console.log(keysArray);
The above example will give the following output:
['name', 'age', 'city']
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.