23 November 2023

In JavaScript, you can count the number of keys in an object using the Object.keys() method and then .length is used to get the number of keys in the object.

Source code viewer
  1. const myObject = {
  2. key1: 'value1',
  3. key2: 'value2',
  4. key3: 'value3'
  5. };
  6.  
  7. const numberOfKeys = Object.keys(myObject).length;
  8.  
  9. console.log(numberOfKeys);
  10. // Output:u 3
Programming Language: ECMAScript