JavaScript Object.keys( ) Function - GeeksforGeeks

30 Nov, 2021
In this article, we will learn the Object.keys() method in Javascript, along with understanding its implementation through the examples.
Object and Object Constructors in JavaScript: In object-oriented programming, JavaScript has the concept of objects and constructors that work mostly in the same manner & can perform similar kinds of operations, likewise in other programming languages. An Object in JavaScript may be defined as an unordered collection of related data, of primitive or reference types, in the form of “key: value” pairs. These keys can be variables or functions and are called properties and methods, respectively, in the context of an object.
Constructors are general JavaScript functions used with the new keyword & have two types i.e. built-in constructors(array and object) and custom constructors(defined properties and methods for specific objects). Constructors can be useful to create an object “type” that can be used multiple times without having to redefine the object every time and this could be achieved using the Object Constructor function. It’s a convention to capitalize the name of constructors to distinguish them from regular functions.
For instance, consider the following code:
function Automobile(color) {
 this.color=color;
}
var vehicle1 = new Automobile ("red");
The function “Automobile()” is an object constructor, and its properties and methods i.e “color” is declared inside it by prefixing it with the keyword “this”. Objects defined using an object constructor are then made instants using the keyword “new”. When new Automobile() is called, JavaScript does two things:
It creates a fresh new object(instance) Automobile() and assigns it to a variable.
It sets the constructor property i.e “color” of the object to Automobile.
Object.keys() Method: The Object.keys() method is used to return an array whose elements are strings corresponding to the enumerable properties found directly upon an object. The ordering of the properties is the same as that given by the object manually in a loop is applied to the properties. Object.keys() takes the object as an argument of which the enumerable own properties are to be returned and returns an array of strings that represent all the enumerable properties of the given object.
Syntax:
Object.keys(obj);
Parameter value:
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.
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.
We will understand the concept of the above function through the examples.
Example 1: In this example, an array “check” has three property values [‘x’, ‘y’, ‘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.
Javascript
<script>    // Returning enumerable properties  // of a simple array   var check = ['x', 'y', 'z'];  console.log(Object.keys(check));</script>
Output:
['0', '1', '2']
Example 2: In this example, 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.
Javascript
<script>    // Returning enumerable properties  // of an array like object.  var object = { 0: 'x', 1: 'y', 2: 'z' };  console.log(Object.keys(object));</script>
Output:
['0', '1', '2']
Example 3: In this example, an array-like object “check” has three property values { 70: ‘x’, 21: ‘y’, 35: ‘z’ } in random ordering and the object.keys() method returns the enumerable properties of this array in the ascending order of the value of indices.
Javascript
<script>    // Returning enumerable properties of an array  // like object with random key ordering.  var object = { 70: 'x', 21: 'y', 35: 'z' };  console.log(Object.keys(object));</script>
Output:
['21', '35', '70']
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.
Supported Browser:
Google Chrome 5.0
Microsoft Edge 12.0
Firefox 4.0
Internet Explorer 9.0
Opera 12.0
Safari 5.0
ysachin2314
bhaskargeeksforgeeks
javascript-functions
javascript-object
JavaScript
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Difference Between PUT and PATCH Request
JavaScript | console.log() with Examples
Node.js | fs.writeFileSync() Method
Set the value of an input field in JavaScript
How to read a local text file using JavaScript?