Quick Overview
Object keys are always strings, so `obj[1]` and `obj["1"]` reach the same slot. `?.` stops a lookup at the first `null` or `undefined` rather than throwing on the property after it.
An object is an unordered collection of related data stored as key: value property pairs enclosed in curly braces {...}.
Objects model real-world entities (users, products, configurations) in application state.
Access object properties using dot notation (user.name) or bracket notation (user["name"]) when keys contain spaces or are stored in dynamic variables:
const user = {
name: "Sarah",
role: "Developer",
"active status": true,
};
// Dot notation
console.log(user.name); // "Sarah"
// Bracket notation (Required for spaces or dynamic variables)
console.log(user["active status"]); // true
const keyToRead = "role";
console.log(user[keyToRead]); // "Developer"
// Updating & Adding Properties
user.role = "Lead Developer";
user.location = "San Francisco";
Reading a property from a non-existent object (e.g. null.profile) throws an uncaught TypeError: Cannot read properties of null. Use optional chaining (?.) to safely return undefined instead of crashing:
const userProfile = { name: "Alex" };
// Safe optional chaining
console.log(userProfile.address?.city); // undefined (No crash!)
user.keyToRead searches for a property literally named "keyToRead". Use bracket notation user[keyToRead] when reading dynamic variable keys!{ a: 1 } === { a: 1 } evaluates to false because they occupy different memory addresses.Write it yourself. The tests below run against your code.
Objects structure entity data. Next, let's explore functional array transformations starting with .map()!
Primitives copy by value and objects copy by reference, so `b = a` duplicates a number but shares an object. That single distinction explains most surprise mutations in a codebase.
`const` prevents reassignment, not mutation: a `const` object's properties can still be changed. `let` is scoped to its block, while `var` is function-scoped and leaks out of loops.
Only backticks interpolate: `${user}` reads the variable, while the identical text in quotes prints literally. Strings are immutable, so every method returns a new one rather than editing in place.