Objects store data as key-value pairs. They represent things — a user, a product, an API response, a configuration. If arrays are ordered lists, objects are labeled containers.
const user = { name: "Sarah", age: 28, role: "admin" };// Dot notation — use when you know the property nameconsole.log(user.name); // "Sarah"console.log(user.age); // 28// Bracket notation — use with variables or special charactersconsole.log(user["role"]); // "admin"const field = "name";console.log(user[field]); // "Sarah" — dynamic access
Use dot notation by default — it’s cleaner and easier to read. Use bracket notation when the property name is stored in a variable or contains characters like hyphens or spaces.
When you’re not sure a nested property exists, use ?. to avoid errors:
Copy
const user = { name: "Sarah", address: null };// ❌ Without optional chaining — crashesconsole.log(user.address.city); // TypeError: Cannot read properties of null// ✅ With optional chaining — returns undefinedconsole.log(user.address?.city); // undefinedconsole.log(user.preferences?.theme); // undefined
API responses often have missing or null fields. Use optional chaining ?. whenever you access nested properties from external data. It prevents “Cannot read properties of undefined” — one of the most common JavaScript errors.
// Delete a propertydelete user.role;console.log(user.role); // undefined
Remember: even though user is declared with const, you can still modify its properties. const prevents reassigning the variable itself (user = something), not modifying the object’s contents. We covered this in the Variables lesson.
const user = { name: "Sarah", email: "sarah@example.com", age: undefined };// "in" operator — checks if the key exists (even if value is undefined)console.log("name" in user); // trueconsole.log("role" in user); // falseconsole.log("age" in user); // true (key exists, value is undefined)// Comparing to undefined — can't distinguish "missing" from "set to undefined"console.log(user.role !== undefined); // falseconsole.log(user.age !== undefined); // false (misleading!)
Use the in operator when you need to know if a key exists, regardless of its value. Use direct comparison when you just care about whether the value is usable.
When a variable name matches the property name, you can use a shorthand:
Copy
const name = "Sarah";const age = 28;const role = "admin";// ❌ Long formconst user = { name: name, age: age, role: role };// ✅ Shorthand — same resultconst user = { name, age, role };console.log(user); // { name: "Sarah", age: 28, role: "admin" }
You’ll see this pattern everywhere — especially when returning data from functions or building objects from variables.
Objects can contain functions. When a function is on an object, it’s called a method:
Copy
const calculator = { add(a, b) { return a + b; }, subtract(a, b) { return a - b; }, multiply(a, b) { return a * b; },};console.log(calculator.add(5, 3)); // 8console.log(calculator.multiply(4, 7)); // 28
In this course, you won’t write many object methods yourself. React components use functions and hooks instead. But you’ll see methods on built-in objects (Math.round(), JSON.parse(), console.log()) and on API response objects.