JavaScript Object.keys()
Example
// Create an Object
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
// Get the Keys
const keys = Object.keys(person);
Try it Yourself »
More Examples Below !
Description
The Object.keys()
method returns an array with the keys of an object.
The Object.keys()
method does not change the original object.
Related Methods:
Object.keys() returns the keys (properties) of any object type.
Object.values() returns the values of all object keys (properties).
Object.entries() returns the keys and values of any object types.
The methods above return an Iterable (enumerable array).
Iterables makes it simpler to use objects in loops and to convert objects into maps.
Syntax
Object.keys(object)
Parameters
Parameter | Description |
object | Required. An iterable object. |
Return Value
Type | Description |
Array | An array containing the keys of the object. |
More Examples
Examples
This example list only the enumerable properties of an object:
// Create an Object
const person = {
firstName: "John",
lastName: "Doe",
age: 50,
eyeColor: "blue"
};
// Change Property
Object.defineProperty(person, "age", {enumerable:false});
// Get the Keys
const keys = Object.keys(person);
Try it Yourself »
Use Object.keys() on an array:
const fruits = ["Banana", "Orange", "Apple", "Mango"];
const keys = Object.keys(fruits);
Try it Yourself »
Use Object.keys() on a string:
const fruits = "Banana";
const keys = Object.keys(fruits);
Try it Yourself »
Browser Support
Object.keys()
is an ECMAScript5 (ES5) feature.
ES5 (JavaScript 2009) is fully supported in all modern browsers since July 2013:
Chrome 23 |
IE/Edge 11 |
Firefox 21 |
Safari 6 |
Opera 15 |
Sep 2012 | Sep 2012 | Apr 2013 | Jul 2012 | Jul 2013 |