JavaScript ECMAScript 2024 ·
✓ verified by execution on
Introduction to Objects
As your programs grow more complex, you’ll need ways to organize related pieces of data. In JavaScript, an object is a collection of properties, and a property is an association between a name (or key) and a value. You can think of an object as a container that groups related variables together under a single name.
You can create an object using an object initializer (also known as an object literal). This is simply a comma-separated list of key-value pairs wrapped in curly braces {}.
javascript
const user = { name: 'Alice', age: 30 };console.log(user.name);
Output
Alice
Your output
In the example above, we created a user object with two properties: name and age. We then accessed the name property using dot notation (user.name).
Methods: Functions Inside Objects
A property’s value can be any JavaScript data type, including strings, numbers, booleans, arrays, or even other objects. Most importantly, a property’s value can be a function! When a function is attached to an object, the property is known as a method.
When you create objects using object initializers, identical object initializers create distinct objects that do not compare to each other as equal. This is because JavaScript compares objects by memory reference, not by their internal values.
Even though obj1 and obj2 look exactly the same, they live in different places in memory!
Two object literals with identical contents are still two separate objects at different memory addresses, which is why comparing them with === is false.
String Keys and Bracket Notation
Each property name before the colon is either an identifier, a number literal, or a string literal. If your property name contains spaces or special characters, you MUST use quotes.
When you use string keys that aren’t valid identifiers, you cannot use dot notation. Instead, you must use bracket notation.
A huge misconception is that dot notation and bracket notation do exactly the same thing. They don’t!
Dot notation expects a literal identifier (like .name). Bracket notation evaluates the expression inside the brackets. This means you can use a variable inside bracket notation to dynamically access a property based on its value!
Predict the outputjavascript
Read the code. What exactly will it print? Commit to an answer before you look.
const book = { title: '1984', author: 'George Orwell' };const propToCheck = 'author';// What will these two statements print?console.log(book.propToCheck);console.log(book[propToCheck]);
Output
undefined
George Orwell
You predicted
Because book.propToCheck looks for a property literally named “propToCheck” (which doesn’t exist), it returns undefined. But book[propToCheck] evaluates the variable first, turning into book['author'], which returns the correct value.
Modifying and Adding Properties
You can easily change the value of an existing property or add entirely new properties to an object on the fly.
Why is bracket notation sometimes required instead of dot notation?
Reveal answer
When the property name contains spaces or is stored in a variable — Dot notation only works with exact string identifiers. If the key has spaces or is dynamic, bracket notation is required.
Are objects with identical keys and values strictly equal (`===`) in JavaScript?
Reveal answer
No, they are distinct memory references — Objects are compared by their memory reference, not their contents. Two distinct objects are never equal.
What happens if you access an object property that does not exist?
Reveal answer
It returns undefined — Accessing a non-existent property returns undefined.
Challenges
Challenge 1 +25 XP
Create an object named `player` with a property `score` set to 100.
javascript
Test 1 — expects "100\n"
Need a hint? (−25% XP)
Use curly braces `{}` to create an object literal.
Show solution (0 XP)
const player = { score: 100 };console.log(player.score);
🐞 Bug Hunt+25 XP
A developer is trying to build a function that dynamically looks up a property of an object based on user input, but it's returning `undefined`. Fix the bug!
This code runs. It just does the wrong thing. Read it, find the defect,
fix it — the tests below decide when you are right.
javascript
Test 1 — expects "blue\n"
Need a hint? (−25% XP)
Dot notation looks for a property exactly matching the text you typed. Put `propName` inside square brackets `[]` after `shirt` to evaluate it dynamically.
Your Object Is Not a Hash Map: Seven Expressions Most People Get Wrong — Use a Map instead of an object is advice everyone has heard and nobody follows, because the reasons are read as a list of properties. Predict what seven expressions evaluate to here, and the reasons stop being a list.