How to Get Keys from an Object in JavaScript

Getting all the keys from an object in javascript

Object.keys is a built-in javascript method used to get all the keys from an object. You just have to pass the object as a parameter. Here is an example.

The output of the above code will be :

Array [“name”, “age”, “profession”, “isActive”]

Using Object.keys method with an array

You can use this method with an array to get all the indexes from an array. An array will be passed as a parameter instead of an object. Here is an example.

The output of the above code will be

Array [“0”, “1”, “2”]

What is the return type of Object.keys?

An array of strings is the return type of object.keys method. This array of strings has all the keys of an object or indexes if an array was passed as a parameter.

For more details please check developer.mozilla.org

Conclusion

Object.keys method is a built-in JavaScript function that can extract all the keys from an object or array. It returns a new array with all the keys of an object or array. It expects a parameter that can be an array or object.

Also check : How to merge two objects in javascript

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here