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.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
