AI Hints
JavaScript · Lesson 58 of 138

Objects

Named values.

⏱ 2 min read▶ Video at 1:11:03🎓 Free
Watch this lesson in the JavaScript courseWatch on YouTube ↗Full JavaScript course

An array is great for a list. But how do you store one thing with many details, like a person's name, age, and city? With an array, you'd have to remember which position is which. An object gives every value a name.

Objects
From the video: Objects
  • One thing, many details
  • Positions are hard to remember
  • Objects name every value

Keys and values

Curly braces make an object. Inside, each entry is a key, a colon, and a value, separated by commas. The keys are the names, and the values can be anything, even arrays. Node prints the whole object neatly. Run it.

app.jsjavascript
const person = {
  name: "Ana",
  age: 29,
  city: "Lisbon",
  hobbies: ["chess", "running"],
};

console.log(person);
console.log(person.name, "lives in", person.city);
Output
{
  name: 'Ana',
  age: 29,
  city: 'Lisbon',
  hobbies: [ 'chess', 'running' ]
}
Ana lives in Lisbon

Objects everywhere

Objects are the main way JavaScript organizes data. A user, a product, a setting, a message from a server, all objects. Arrays themselves are a special kind of object. And when we get to the browser, every element on the page is an object too.

keyvalue
{ key: value }an object
Keysnames, usually camelCase
Valuesany type
Everywhereusers, products, data

Summary

Objects store values under names, called keys. They're the main way to organize data in JavaScript. Next, reading and changing properties.

Key takeaways
  • { name: 'Ana' }
  • Key: value pairs
  • Values can be anything
Next up: Accessing Properties — dot and brackets