Start learning free

LESSON 123 OF 671 · DICTIONARIES

Accessing Dictionary Values in JavaScript

Lesson

In JavaScript, you can access values in an object using bracket notation or dot notation:

const scores = {"Alice": 95, "Bob": 87, "Charlie": 72};

let aliceScore = scores["Alice"];  // 95  (bracket notation)
let bobScore = scores.Bob;         // 87  (dot notation)

Bracket notation works for any key, including keys with spaces or special characters. Dot notation only works for simple keys.

if ("Alice" in scores) {
    console.log(scores["Alice"]);  // 95
}

// Returns undefined if key doesn't exist
console.log(scores["Diana"]);  // undefined

Assignment
Follow the Coding Tutorial and access Alice's score from scores!


Hint
Use scores["Alice"] and assign it to aliceScore.

STEP 1 OF 1
    Ctrl + Enter to check