Start learning free

LESSON 125 OF 671 · DICTIONARIES

Dictionary Length in JavaScript

Lesson

In JavaScript, you can get the number of key-value pairs in an object using Object.keys().length:

const scores = {"Alice": 95, "Bob": 87, "Charlie": 72};
let count = Object.keys(scores).length;
console.log(count);  // 3

Object.keys() returns an array of the object's keys, and .length gives the count.

const d = {a: 1, b: 2};
console.log(Object.keys(d).length);    // 2
console.log(Object.values(d).length);  // 2  (same result)

Assignment
Follow the Coding Tutorial and get the number of entries in scores!


Hint
Use Object.keys(scores).length and assign it to count.

STEP 1 OF 1
    Ctrl + Enter to check