Objects

An object groups related values together under named keys, instead of numbered positions like an array. It's how you model a single "thing" that has several properties — a user, a product, a settings panel.

Object literals

Curly braces create an object, with each property written as key: value:

Try it yourself
Console output

Dot notation vs. bracket notation

Dot notation (car.make) is the everyday way to read or set a property. Bracket notation (car["make"]) does the same thing, but takes the key as a string — which matters when the key is stored in a variable rather than typed directly:

Try it yourself
Console output

car.key would look for a property literally named key, which doesn't exist here. Bracket notation exists for exactly this case — when the property name is only known once the code runs.

Nested objects

Object properties can hold other objects. Chain dots to reach further in:

Try it yourself
Console output

Methods on objects

A function stored as a property is called a method. Inside a method, this refers to the object it was called on — it's how the method reaches the rest of its own data:

Try it yourself
Console output
Note: object property names are always strings behind the scenes, even when you write them without quotes. { make: "Toyota" } and { "make": "Toyota" } create the exact same object.