Skip to content

How to get object length in JavaScript | Code

  • by

Use the Object keys method to get object length in JavaScript. You can also find the length of the values inside the object.

var size = Object.keys(myObj).length;

Strings: You can use the length property of a string to get its length.

const myString = "Hello, World!";
const stringLength = myString.length;
console.log(stringLength); // Outputs: 13

Arrays: Arrays have a length property as well, which gives you the number of elements in the array.

const myArray = [1, 2, 3, 4, 5];
const arrayLength = myArray.length;
console.log(arrayLength); // Outputs: 5

Objects: In JavaScript, objects do not have a built-in length property like strings and arrays. To get the number of properties (keys) in an object, you can use a for...in loop or Object.keys() method.

Using a for...in loop:

const myObject = {
  name: "John",
  age: 30,
  city: "New York"
};

let objectLength = 0;
for (const key in myObject) {
  if (myObject.hasOwnProperty(key)) {
    objectLength++;
  }
}

console.log(objectLength); // Outputs: 3

Using Object.keys():

const myObject = {
  name: "John",
  age: 30,
  city: "New York"
};

const objectKeys = Object.keys(myObject);
const objectLength = objectKeys.length;
console.log(objectLength); // Outputs: 3

Map and Set: You can get the size (number of key-value pairs) of a Map or the number of elements in a Set using their size property.

const myMap = new Map();
myMap.set("key1", "value1");
myMap.set("key2", "value2");

const mapSize = myMap.size;
console.log(mapSize); // Outputs: 2

const mySet = new Set([1, 2, 3, 4, 5]);
const setSize = mySet.size;
console.log(setSize); // Outputs: 5

These are the common ways to get the length or size of various objects in JavaScript. Depending on the type of object you are working with, you can choose the appropriate method to determine its length or size.

How to get object length in JavaScript

A simple example code gets the keys as an array using Object.keys, and takes the length of that array:

<!DOCTYPE html>
<html>
<body>

  <script>
    const obj = {
      namespace: {
        key1: 'whatever1',
        key2: 'whatever2',
      }
    }

    const keys = Object.keys(obj.namespace) 
    const keysLength = keys.length

    console.log(keys)
    console.log(keysLength)
  </script>

</body>
</html> 

Output:

How to get object length in JavaScript

Another example

Find the size of the object (i.e., the total number of attributes in the object) like this:

namelist = { "name":"xyz", "version":"1.0.0" }
var size = Object.keys(namelist).length;
console.log(size); // 2

For getting the size of the value of name attribute

console.log(namelist.name.length) // 3

For getting the size of the value of version attribute.

console.log(namelist.version.length) // 5

Do comment if you have any doubts or suggestions on this Js Object topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *