Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript Object.keys() Function

Improve Article
Save Article
  • Last Updated : 22 Dec, 2022
Improve Article
Save Article

JavaScript Object.keys() function 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 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:

  • 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.

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']

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 Browser:

  • Google Chrome 5.0
  • Microsoft Edge 12.0
  • Firefox 4.0
  • Internet Explorer 9.0
  • Opera 12.0
  • Safari 5.0

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!