DevPath · Learn to code ESPTEN

Objects

Object literals and property access

What is an object?

Think of a user's profile card: it has a name, an email, an age. Or a product: price, stock, category. Each one is a handful of pieces of data that only make sense together. That, exactly, is an object.

An object groups related data into key: value pairs. If an array was a numbered row, an object is the card with labels:

const person = {
  name: "Ana",
  age: 30,
  active: true,
};

Each pair (name: "Ana") is a property: the key is the label and the value, its content. And you can ask that card anything you like.

Dot access

The most common form:

console.log(person.name); // "Ana"
console.log(person.age);  // 30

Bracket access

Useful when the key is stored in a variable or has special characters (spaces, dashes) that dot notation won't accept:

const key = "age";
console.log(person[key]);    // 30
console.log(person["name"]); // "Ana"

Adding and modifying properties

person.email = "ana@mail.com"; // adds
person.age = 31;               // modifies
delete person.active;          // deletes

⚠️ Classic trap: if you ask for a property that doesn't exist, JavaScript won't complain: it just hands you undefined. No red errors, so double-check how you spell the key.

Examples

A book's card: creating it and reading it

const book = {
  title: "Hopscotch",
  author: "Cortázar",
  pages: 600,
};
console.log(book.title);    // "Hopscotch"
console.log(book["author"]); // "Cortázar"
Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
Methods and this →