JavaScript Objects


What is an Object?

In JavaScript, an object is a collection of key-value pairs (also called properties).

Objects help store and organize related data and functions in one place.

Object Syntax

const person = {
  firstName: "Alice",
  lastName: "Johnson",
  age: 30,
  isStudent: false
};
  • firstName, lastName, age, isStudent are keys (properties)
  • "Alice", "Johnson", 30, false are values

Accessing Object Properties

Dot Notation:

console.log(person.firstName); // Output: Alice

Bracket Notation:

console.log(person["lastName"]); // Output: Johnson

Use brackets when:

  • Property name has spaces/special characters
  • You access properties dynamically

Updating & Adding Properties

Update:

person.age = 31;

Add New Property:

person.country = "India";

Deleting Properties

delete person.isStudent;

Object Methods

A method is a function inside an object.

const car = {
  brand: "Toyota",
  model: "Camry",
  start: function () {
    console.log("Engine started");
  }
};

car.start(); // Output: Engine started

You can also use shorthand syntax:

const car = {
  brand: "Toyota",
  start() {
    console.log("Engine started");
  }
};

this Keyword

Inside an object method, this refers to the object itself.

const user = {
  name: "Sara",
  greet() {
    console.log("Hi, I'm " + this.name);
  }
};

user.greet(); // Output: Hi, I'm Sara

Nested Objects

Objects can contain other objects.

const student = {
  name: "John",
  grades: {
    math: 90,
    english: 85
  }
};
                                  
console.log(student.grades.math); // Output: 90

Looping Through Objects

for...in Loop:

const book = {
  title: "1984",
  author: "George Orwell"
};
                                  
for (let key in book) {
  console.log(key + ": " + book[key]);
}

Built-in Object Methods

Object.keys(obj)

Returns an array of keys.

Object.keys(book); // ['title', 'author']

Object.values(obj)

Returns an array of values.

Object.values(book); // ['1984', 'George Orwell']

Object.entries(obj)

Returns an array of [key, value] pairs.

Object.entries(book);
// [['title', '1984'], ['author', 'George Orwell']]

Object Destructuring (ES6)

Extract values into variables easily.

const user = {
  name: "Liam",
  age: 25
};
                                  
const { name, age } = user;
console.log(name); // Liam
console.log(age);  // 25

Object in Arrays

Useful for storing lists of items (e.g., users, products).

const users = [
  { name: "Alice", age: 20 },
  { name: "Bob", age: 25 }
];
                                  
console.log(users[1].name); // Output: Bob

Summary

Feature Example
Create object const obj = { key: value }
Access property obj.key or obj["key"]
Add/update property obj.newKey = value
Delete property delete obj.key
Object method obj.method = function() {}
Use this Inside method: refers to object
Loop object for (let key in obj)
Destructuring const { key } = obj

🧪 Practice Exercise:

Task:

  1. Create an object called movie with keys: title, director, year.
  2. Add a method getDetails that returns a formatted string using this.title, etc.
  3. Loop over the object to print all key-value pairs.
  4. Try destructuring the movie object.