.keys()
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']
Contribute to Docs
- 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.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours