Skip to main content

Command Palette

Search for a command to run...

Essential Insights into JavaScript Objects

JavaScript Objects Explained: Learn and Apply the Fundamentals Effortlessly

Published
5 min read
Essential Insights into JavaScript Objects
M

Hi, I'm Manshi, a full-stack web developer. I write about building web apps and my journey in tech. Follow along as I share what I'm learning and creating.

JavaScript is known for its flexibility and dynamic nature. One of its core features is the use of objects. In everyday language, objects are like real-world items that have characteristics (properties) and perform actions (methods). If you are a beginner or struggling to understand objects, this guide will help you grasp the concept easily using real-world examples.


What Is a JavaScript Object?

An object in JavaScript is a collection of properties, where each property is a key (or name) paired with a value. These values can be primitive data (like strings or numbers), arrays, other objects, or even functions (which we call methods).

Imagine you have a smartphone.

  • Properties: Your smartphone has a brand, model, color, and storage capacity.

  • Actions (Methods): It can make calls, send texts, and play music.

In JavaScript, an object works similarly: it’s a collection of properties (data) and methods (functions) that act on that data.

Example:
A simple smartphone object might look like this:

const smartphone = {
  brand: "Apple",
  model: "iPhone 14",
  color: "black",
  storage: "128GB",
  makeCall: function(number) {
    console.log(`Calling ${number}...`);
  }
};

Why Use Objects?

Objects help us organize and structure data efficiently. Instead of using multiple variables for related data, we store them in a single object.

For example, instead of:

let name = "Alice";
let age = 25;
let profession = "Developer";

We can use an object:

let person = {
    name: "Alice",
    age: 25,
    profession: "Developer"
};

This approach makes code cleaner and easier to manage.


Creating an Object in JavaScript

There are different ways to create an object:

1. Using Object Literal

This is the most common and easiest way:

let student = {
    name: "John",
    age: 20,
    course: "Computer Science"
};

2. Using the new Object() Constructor

let employee = new Object();
employee.name = "Sarah";
employee.position = "Manager";
employee.salary = 50000;

3. Using a Constructor Function

If we want to create multiple objects with similar properties, we can use a function:

function Book(title, author, year) {
    this.title = title;
    this.author = author;
    this.year = year;
}
let book1 = new Book("Harry Potter", "J.K. Rowling", 1997);
let book2 = new Book("Lord of the Rings", "J.R.R. Tolkien", 1954);

4. Using ES6 Classes

Classes provide a modern way to create objects:

class Animal {
    constructor(name, species) {
        this.name = name;
        this.species = species;
    }
}
let dog = new Animal("Buddy", "Dog");
let cat = new Animal("Whiskers", "Cat");

Accessing Object Properties

We can access object properties using dot notation or bracket notation.

1. Dot Notation

console.log(car.brand);  // Output: Toyota
console.log(person.name); // Output: Alice

2. Bracket Notation

console.log(car["model"]);  // Output: Corolla
console.log(person["age"]); // Output: 25

Bracket notation is useful when property names have spaces or special characters.


Modifying Object Properties

You can modify an object's properties by directly assigning new values.

car.color = "Red";
console.log(car.color); // Output: Red

Adding New Properties

You can add new properties to an object by simply assigning values to new keys.

person.country = "USA";
console.log(person); // Output: { name: "Alice", age: 25, profession: "Developer", country: "USA" }

Deleting Properties

To remove a property, use the delete keyword:

delete person.age;
console.log(person);

Object Methods

Objects can have methods, which are functions stored as properties.

Example:

let student = {
    name: "Emma",
    age: 22,
    greet: function() {
        return "Hello, my name is " + this.name;
    }
};
console.log(student.greet()); // Output: Hello, my name is Emma

this.name refers to the name property inside the same object.


Object Iteration

We can loop through an object using a for...in loop:

for (let key in car) {
    console.log(key + ": " + car[key]);
}

This loop will print each key-value pair of the car object.


Object.keys(), Object.values(), Object.entries()

These methods allow us to work with object properties more easily:

  • Object.keys(object): Returns an array of keys.

  • Object.values(object): Returns an array of values.

  • Object.entries(object): Returns an array of key-value pairs.

Example:

console.log(Object.keys(car));   // ["brand", "model", "color", "year"]
console.log(Object.values(car)); // ["Toyota", "Corolla", "Blue", 2022]
console.log(Object.entries(car)); // [["brand", "Toyota"], ["model", "Corolla"], ["color", "Blue"], ["year", 2022]]

Nested Objects

Objects can contain other objects:

let company = {
    name: "TechCorp",
    location: "New York",
    ceo: {
        name: "Michael",
        age: 45
    }
};
console.log(company.ceo.name); // Output: Michael

Object Destructuring

Object destructuring allows you to extract values from an object into distinct variables, making your code more concise.

const person = {
  name: "Alice",
  age: 25,
  profession: "Developer"
};

// Destructuring
const { name, age, profession } = person;

console.log(name);        // Output: Alice
console.log(age);         // Output: 25
console.log(profession);  // Output: Developer

You can also rename variables during destructuring:

const { name: fullName, age: yearsOld } = person;

console.log(fullName);    // Output: Alice
console.log(yearsOld);    // Output: 25

Rest/Spread Operator

The spread operator (...) allows you to copy properties from one object to another or merge multiple objects together. Similarly, the rest operator (...) helps collect remaining properties.

Spread Operator

const person = { name: "Alice", age: 25, profession: "Developer" };
const newPerson = { ...person, country: "USA" };

console.log(newPerson);
// Output: { name: "Alice", age: 25, profession: "Developer", country: "USA" }

Rest Operator

You can also use the rest operator to collect the remaining properties when destructuring an object:

const person = { name: "Alice", age: 25, profession: "Developer" };
const { name, ...rest } = person;

console.log(name);  // Output: Alice
console.log(rest);  // Output: { age: 25, profession: "Developer" }

If you found this helpful, drop a ❤️! Have any questions or topics you'd like to learn next? Share them in the comments! Happy coding!