Today, In the article, you are going to learn how to find the length of a JavaScript object. Suppose, you have the following object.
let obj = {
name:"John",
age:40
};
Now, to find the length of the above object, we will use the Object.keys() method. The Object.keys() returns an array with all the properties of the object.
If you run the code in your code editor, you will get the following output.
console.log(Object.keys(obj)) /* output ▶️Array 0:name 1:age length:2 */
If you notice the above output, you can see the length property. It gives the length of the object.
Syntax #
var size = Object.keys(objectName).length
Comments (0)