Objects and Properties

Beginner
80 XP
30 min
Lesson Content

Objects and Properties

Objects store data as key-value pairs. They're perfect for representing real-world entities like users, products, or settings.

Creating Objects

const person = {
  name: 'John',
  age: 30,
  city: 'New York'
};

Accessing Properties

// Dot notation
person.name; // 'John'

// Bracket notation
person['age']; // 30

Modifying Objects

// Add property
person.email = 'john@example.com';

// Update property
person.age = 31;

// Delete property
delete person.city;

Object Methods

const person = {
  name: 'John',
  greet: function() {
    return 'Hello, I am ' + this.name;
  }
};
Example Code

Create and manipulate objects

const person = {
  name: 'Sam',
  age: 28,
  city: 'San Francisco'
};

// Access properties
console.log('Name:', person.name);
console.log('Age:', person.age);

// Add new property
person.email = 'sam@example.com';
console.log('Email:', person.email);

// Update property
person.age = 29;
console.log('Updated Age:', person.age);

// Object with method
const car = {
  brand: 'Toyota',
  model: 'Camry',
  getInfo: function() {
    return this.brand + ' ' + this.model;
  }
};
console.log(car.getInfo());

Expected Output:

Name: Sam
Age: 28
Email: sam@example.com
Updated Age: 29
Toyota Camry
Study Tips
  • Read the theory content thoroughly before practicing
  • Review the example code to understand key concepts
  • Proceed to the Practice tab when you're ready to code