Navigate back to the homepage

How to get number of keys of object in javascript?

Infinitbility
JavaScript
May 5th, 2022 · 1 min read
keep affilitate

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

  1. create a sample object variable
  2. use Object.keys() method to get an array
  3. use .length to get the number of keys
1// create sampel oject variable
2let sampleObject = {
3 id: 1,
4 name: "infinitbility",
5 domain: "infinitbility.com",
7};
8
9// convert keys to array
10let sampleObjectKeys = Object.keys(sampleObject);
11console.log("sampleObjectKeys", sampleObjectKeys)
12
13// print number of keys
14console.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

javascript, get number of keys
javascript, get number of keys

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.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to push array of objects in another array in javascript?

Example of push array of objects in another array in javascript with step by step guide.

May 4th, 2022 · 1 min read

How to add array to another array in javascript?

Example of add array to another array in javascript with step by step guide.

May 3rd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]