Lauro Silva

Watch the video

NEW

Prefer your lessons in video format? Watch for free on egghead:

Let’s start by creating an Object.

1const pokemon = {}

This Object will contain the following properties: name, type, level.

1const pokemon = {
2 name: "Charmander",
3 type: "🔥",
4 level: 400,
5}

We then console.log the Object and try to get the length of the Object by using the length property.

1console.log(pokemon)
2console.log(pokemon.length)

This returns undefine. Instead, we use Object.keys() method to return an array of the enumerable property names.

1console.log(Object.keys(pokemon))

Then we determine the length of the array, by using the length property on the enumerable property names.

1const pokemon = {
2 name: "Charmander",
3 type: "🔥",
4 level: 400,
5 color: "orange",
6}
7
8console.log(Object.keys(pokemon).length)

© Lauro Silva, LLC. All rights reserved.