Open In App

JavaScript Object keys() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The Object.keys() method in JavaScript is used to retrieve an array of the enumerable property names of an object. It returns an array containing the keys of the object.

Syntax:

Object.keys(obj);

Parameter:

  • obj: It is the object whose enumerable properties are to be returned.

Return Value:

It returns an array of strings that represent all the enumerable properties of the given object.

Example 1: Enumerating Array Indices with Object.keys()

The code uses the Object.keys() method to retrieve the enumerable properties of the array [‘x’, ‘y’, ‘z’] and logs them to the console. Since arrays in JavaScript are also objects, their indices are treated as properties. Therefore, the output will be [‘0’, ‘1’, ‘2’].

// Returning enumerable properties
// of a simple array 
let check = ['x', 'y', 'z'];
console.log(Object.keys(check));

Output
[ '0', '1', '2' ]

Example 2: Enumerating Array-Like Object Properties with Object.keys()

Here, an array-like object “check” has three property values { 0: ‘x’, 1: ‘y’, 2: ‘z’ } and the object.keys() method returns the enumerable properties of this array. The ordering of the properties is the same as that given by the object manually.

// Returning enumerable properties
// of an array like object.
let object = { 0: 'x', 1: 'y', 2: 'z' };
console.log(Object.keys(object));

Output
[ '0', '1', '2' ]

Applications:

It can be used for returning enumerable properties of a simple array, an array-like object & an array-like object with random key ordering.

Exceptions:

  • It causes a TypeError if the argument passed is not an object.
  • If an object is not passed as an argument to the method, then it persuades it and treats it as an object.

We have a complete list of JavaScript Object methods, to check those please go through this JavaScript Object Complete Reference article.

Supported Browsers:

JavaScript Object keys() Method- FAQs

What does the Object.keys() method do in JavaScript?

The Object.keys() method returns an array of a given object’s own enumerable property names (keys). The keys are returned in the same order as they would be in a normal loop.

Does Object.keys() include inherited properties?

No, Object.keys() only returns the object’s own enumerable property names. Inherited properties are not included.

How does Object.keys() handle primitive data types?

If a non-object argument is passed to Object.keys(), it will be coerced to an object. For example, passing a string will return an array of its indices as strings.

How does Object.keys() handle properties with null or undefined values?

Object.keys() includes property names with null or undefined values in the returned array, as it only considers the keys and not the values.

What is the most common use of the Object.keys() method?

The most common use of the Object.keys() method is to iterate over an object’s own enumerable property names. This is particularly useful when you need to perform operations on each property of an object, such as transforming or filtering data. By obtaining an array of keys, you can easily loop through the keys and access or manipulate the corresponding values.


Next Article

Similar Reads

three90RightbarBannerImg