How to use Object.keys() method In Javascript
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.
const array = [1, 2, 3, 4, 5];
const length = Object.keys(array).length;
console.log(length); // Output: 5
Output
5
Explanation:
The above code initializes an array array
containing elements. It converts the array indices into keys using Object.keys()
method and then calculates the length of the resulting array of keys. Finally, it prints the length of the original array.
JavaScript Program to Determine the Length of an Array
Finding the length of an array in JavaScript refers to the process of determining the number of elements contained within an array.
Below are the approaches used to Determine the length of an array in JavaScript:
Here, we will use the length property of the array to determine the length of the array. It will return the number of elements in an array.