Skip to main content

What are objects?

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.
JavaScript objects are like Python dictionaries, but keys don’t need quotes (unless they contain special characters or spaces).

Accessing properties

Two ways to access object properties:
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.

Accessing nested properties

Optional chaining

When you’re not sure a nested property exists, use ?. to avoid errors:
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.

Adding and updating properties

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.

Checking if a property exists

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.

Iterating over objects

Object.entries() is JavaScript’s equivalent of Python’s .items().

Shorthand property names

When a variable name matches the property name, you can use a shorthand:
You’ll see this pattern everywhere — especially when returning data from functions or building objects from variables.

Methods (functions on objects)

Objects can contain functions. When a function is on an object, it’s called a method:
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.

What’s next?

You can create and work with objects. Now let’s learn destructuring — a cleaner way to extract values from objects and arrays.

Destructuring

Extract values into individual variables