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
- Python
Accessing properties
Two ways to access object properties:Accessing nested properties
Optional chaining
When you’re not sure a nested property exists, use?. to avoid 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
Iterating over objects
- JavaScript
- Python
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: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