Bracketball explainer

Aim of the puzzle: Use bracket notation to access an object’s property.
Walk through of solution: Up until now, you have used dot notation to access properties in an object. For example:

let dog = {
  name: 'maisie'
  age: 14
};
console.log(dog.age)

Bracket notation is another way to access the same property. Accessing dog.age in bracket notation would look like dog['age'], with the property name written as a string inside the brackets.

Why use bracket notation?

Generally, dot notation is preferred. However, bracket notation allows you to use a variable inside the brackets, which does not work with dot notation.

For example:

let dog = {}
let variable = 'age';
console.log(dog[variable])

This code is the same as writing dog['age'], because variable holds the string 'age'.

However, this does not work:

let dog = {}
let variable = 'age';
console.log(dog.variable)

This will print undefined, because the code is looking for a property in dog named variable, which does not exist.

In this puzzle, create a new variable and give it either the string 'winner' or 'score' as a value. Then, use the variable with bracket notation inside a console.log() to print the property to the console.
Sample code solution:

let game = {
    winner: 'Grace',
    score: '15 to 10',
};

let variable = 'score';

console.log(game[variable]);

Javascript Concepts: Objects, Bracket Notation, Properties