JavaScript .keys()
The Object.keys() JavaScript method returns an array of a given object’s own enumerable string-keyed property names. This static method extracts only the keys from an object, providing a convenient way to iterate over or analyze object properties without accessing their values.
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 of JavaScript Object.keys()
Object.keys(obj)
Parameters:
obj: The object whose enumerable string-keyed property names are to be returned.
Return value:
Returns an array of strings representing the given object’s own enumerable string-keyed property keys.
Example 1: Basic Usage of Object.keys()
This example demonstrates the fundamental usage of Object.keys() with a simple object containing different property types:
// Create a person objectconst person = {firstName: 'John',lastName: 'Doe',age: 30,city: 'New York',};// Get all keys from the objectconst keys = Object.keys(person);console.log(keys);
This example results in the following output:
["firstName", "lastName", "age", "city"]
The method returns an array containing all enumerable property names from the object. In this case, since all keys are strings, they appear in the order they were defined. However, if the object contained numeric keys, those would be ordered numerically first, regardless of definition order.
Example 2: Using Object.keys() to iterate Over Object Properties
This example shows how to use Object.keys() to iterate through object properties, which is useful for processing or transforming object data:
// Product inventory objectconst inventory = {laptops: 45,smartphones: 123,tablets: 67,headphones: 89,};// Iterate through all product keysObject.keys(inventory).forEach((product) => {console.log(`${product}: ${inventory[product]} units`);});// Alternative: using for...of loopfor (const product of Object.keys(inventory)) {if (inventory[product] < 50) {console.log(`Low stock alert: ${product}`);}}
This example results in the following output:
laptops: 45 unitssmartphones: 123 unitstablets: 67 unitsheadphones: 89 unitsLow stock alert: laptops
Codebyte Example: Filtering and Transforming Objects Using Object.keys()
This example demonstrates using Object.keys() to filter object properties and create new objects based on specific criteria:
Frequently Asked Questions
1. What is the difference between map keys and object keys?
In JavaScript:
- Object keys are always strings (or symbols). Even if you use a number or an object as a key, it gets converted to a string.
- Map keys can be of any type, such as string, number, object, or even functions. They’re not converted to strings. This makes
Mapmore flexible for key-value pairs where key type matters.
2. What is Object.entries and Object.keys?
Object.keys(obj)returns an array of the keys (property names) in the object.Object.entries(obj)returns an array of key-value pairs from the object.
3. Can an object key be a number?
Not exactly. When you use a number as a key, JavaScript automatically converts it to a string.
4. Are object keys ordered?
Yes, in a specific way:
- Integer-like keys come first (sorted numerically).
- Then, string keys appear in insertion order.
- Symbol keys come last, also in insertion order.
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
- Front-end engineers work closely with designers to make websites beautiful, functional, and fast.
- Includes 34 Courses
- With Professional Certification
- Beginner Friendly.115 hours
- Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
- Beginner Friendly.15 hours