This write-up will present an in-depth overview of the below-listed concepts:
- What is a JavaScript Object?
- What is Object.keys() in JavaScript?
- Syntax of Object.keys() method
- How to use Object.keys() in JavaScript?
So, let’s begin!
What is a JavaScript Object?
The Object is a class in JavaScript that can store different key-value pairs or keyed collections. It can store complex entities as well. In JavaScript, the Object class offers several built-in methods that can be used to achieve different functionalities.
What is Object.keys() in JavaScript?
Object.keys() is a built-in static method of the Object class. It takes an object from the user as a parameter and returns an array of strings that consists of all the enumerable properties’ names of the given/user-specified object.
Note: Don’t get confused by the term “enumerable properties”, because, in JavaScript, every property created using a property initializer or created by a simple assignment is by default enumerable. The enumerable properties in JavaScript can be seen using the Object.Keys() function.
Syntax of Object.keys() method
Here is the basic syntax of the Object.keys() method in JavaScript:
In the above snippet, objectName is a user-specified object whose enumerable properties will be returned by the Object.keys() method.
How to use Object.keys() in JavaScript?
As of now, we have understood all the fundamentals of the Object.keys() method, now it’s time to implement these concepts practically.
Example: How to get keys of an object using the Object.keys() method
In this example, we will utilize the Object.keys() method to get all the “keys” of a user-specified object named “empDetails”:
empName: "Joe",
empId: 13,
empAge: 27,
gmailAddress: "[email protected]",
};
var getEmpkeys = Object.keys(empDetails);
console.log("All keys of 'empDetails' object: ", getEmpkeys);
In the above-given program, we performed the following tasks:
- Firstly, we created an object named empDetails and assigned it some properties and values.
- Afterward, we utilized the Object.keys() method and we passed an object named “empDetails” to it.
Consequently, we will get the below-given output:
The output verified that the Object.keys() returned an array of Strings that consists of all the keys of the “empDetails” object.
Example: How to get keys of the randomly ordered objects using the Object.keys() method
In this program, we will consider a scenario where each key is assigned with a random numeric value:
var result = Object.keys(empData);
console.log(result);
In the above-given program, firstly, we created an object named “empData” and assigned it some properties and values. Next, we utilized the Object.keys() method and passed it “empData” as a parameter:
The output verified that the Object.keys() method returned an array of keys.
Conclusion
The Object.keys() is one of the built-in static methods of the Object class that takes an object from the user as a parameter and returns an array of strings. The returned array consists of all the enumerable properties’ names of the given/user-specified object. All in all, the Object.keys() method takes an object from the user and returns all the keys of that object. In JavaScript, the enumerable properties in JavaScript can be seen with the help of the Object.Keys() function. This write-up explained different aspects of the Object.Keys() method using a couple of examples.