Hi Friends 👋,
Welcome To Infinitbility! ❤️
Today, we are going to learn How to get a number of keys of objects in javascript, here we will use the keys()
method and array length to get a number of keys.
Basically, The keys()
method creates an array of object keys, and when we got an array we will use simple array length syntax to get a number of keys.
Well, let’s start today’s tutorial How to get the number of keys of the object in javascript?
Here, we will do
- create a sample object variable
- use
Object.keys()
method to get an array - use
.length
to get the number of keys
1// create sampel oject variable2let sampleObject = {3 id: 1,4 name: "infinitbility",5 domain: "infinitbility.com",7};89// convert keys to array10let sampleObjectKeys = Object.keys(sampleObject);11console.log("sampleObjectKeys", sampleObjectKeys)1213// print number of keys14console.log("number of keys", sampleObjectKeys.length);
we can also write in one line like the below example.
1console.log(Object.keys(sampleObject).length);
lets run it, it should show 4
number of keys.
Output

All the best 👍.
Follow me on Twitter
Join our email list and get notified about new content
No worries, I respect your privacy and I will never abuse your email.
Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.