Day 5: Mastering JavaScript Objects - A Core Skill for Software Engineers

JavaScript objects are fundamental to how the language operates and are essential for writing efficient, scalable code. In today’s post, we’ll explore what JavaScript objects are, their role in software development, how to create them, the different types of objects, object methods, and the use of arrays within objects. We’ll also walk through some practical examples.

What Are JavaScript Objects?

In simple terms, a JavaScript object is a collection of key-value pairs. Unlike arrays, which use numerical indexes, objects use named keys (properties) to store and retrieve values. Think of objects as containers that hold related data and functions (called methods) about a specific entity.

Why Objects Are Important for Software Engineers

Objects are essential because they allow developers to model real-world entities, such as users, products, or events, in a structured way. They are incredibly flexible and provide the backbone for most modern software architectures, from frontend interfaces to backend APIs. Whether you're building web apps or managing large datasets, objects help you group data logically and manage it effectively.

How to Create JavaScript Objects

There are several ways to create an object in JavaScript, but the most common method is using object literals, where you define the properties and their values inside curly braces {}.

let car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2020
};

In this example, the car object has three properties: brand, model, and year.

You can also use the new Object() constructor, although object literals are preferred for simplicity:

let person = new Object();
person.name = "Alice";
person.age = 28;
person.job = "Designer";

Types of JavaScript Objects

In JavaScript, objects can be classified into different types based on their structure and purpose:

  1. Plain Objects: These are the most basic type of objects and are created using the object literal notation or the new Object() constructor.

  2. Function Objects: Functions in JavaScript are also objects. They can have properties and methods just like other objects.

  3. Array Objects: Arrays are technically a type of object, and they come with their own set of methods like push(), pop(), and filter().

  4. Built-in Objects: JavaScript comes with several built-in objects, such as Date, Math, and RegExp, each providing specialized functionality.


JavaScript Object Methods

Objects in JavaScript can have methods, which are functions that operate on the data stored in the object. Here are a few essential object methods every developer should know:

  • Object.keys(): Returns an array of an object’s property names.

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

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

  • hasOwnProperty(): Checks if an object has a specific property.

  • Object.assign(): Copies properties from one or more objects into a target object.

Example:

let user = {
  username: "Stanley24",
  email: "stanley@example.com",
  age: 25
};

let keys = Object.keys(user); // ["username", "email", "age"]
let values = Object.values(user); // ["Stanley24", "stanley@example.com", 25]

Arrays in JavaScript Objects

You can also store arrays within objects to handle collections of related data. For example, an object representing a student could have an array of courses:

let student = {
  name: "John Doe",
  age: 21,
  courses: ["Math", "Science", "English"]
};

console.log(student.courses[0]); // Outputs: "Math"

Arrays in objects are especially useful when you need to represent multiple items related to a single entity.

Example of JavaScript Object Code

Let’s bring everything together with a practical example. Below is a sample code of a JavaScript object that contains properties, an array, and methods:

let library = {
  name: "City Library",
  location: "Downtown",
  books: ["1984", "Brave New World", "The Catcher in the Rye"],

  displayBooks: function() {
    console.log("Available books: " + this.books.join(", "));
  },

  addBook: function(newBook) {
    this.books.push(newBook);
    console.log(`${newBook} has been added to the library.`);
  }
};

// Accessing properties
console.log(library.name); // Outputs: City Library

// Calling methods
library.displayBooks(); 
// Outputs: Available books: 1984, Brave New World, The Catcher in the Rye

library.addBook("To Kill a Mockingbird");
// Outputs: To Kill a Mockingbird has been added to the library.

In this example, we have:

  • A library object with properties like name, location, and books.

  • A method displayBooks() to list all the books.

  • A method addBook() to add a new book to the books array.

Learn More: Free JavaScript Resources

If you're interested in deepening your understanding of JavaScript objects and other JavaScript concepts, check out these free resources:

These resources can help you gain a deeper understanding of how to work with JavaScript objects and improve your coding skills.


10
Subscribe to my newsletter

Read articles from Stanley Owarieta directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Stanley Owarieta
Stanley Owarieta

𝗔𝘀𝗽𝗶𝗿𝗶𝗻𝗴 𝗙𝗿𝗼𝗻𝘁𝗲𝗻𝗱 𝗗𝗲𝘃𝗲𝗹𝗼𝗽𝗲𝗿 passionate about 𝙅𝙖𝙫𝙖𝙎𝙘𝙧𝙞𝙥𝙩, 𝙘𝙤𝙙𝙞𝙣𝙜, 𝙖𝙣𝙙 𝙗𝙪𝙞𝙡𝙙𝙞𝙣𝙜 𝙢𝙮 𝙛𝙪𝙩𝙪𝙧𝙚 𝙞𝙣 𝙩𝙚𝙘𝙝. Along with my 𝙡𝙤𝙫𝙚 for 𝙛𝙖𝙨𝙝𝙞𝙤𝙣, 𝙜𝙖𝙢𝙞𝙣𝙜, 𝙖𝙣𝙙 𝙡𝙪𝙭𝙪𝙧𝙮 𝙡𝙞𝙫𝙞𝙣𝙜, I have big dreams like 𝙤𝙬𝙣𝙞𝙣𝙜 𝙖 𝙥𝙧𝙞𝙫𝙖𝙩𝙚 𝙟𝙚𝙩 and 𝙡𝙞𝙫𝙞𝙣𝙜 𝙞𝙣 𝙖 𝙡𝙪𝙭𝙪𝙧𝙮 𝙝𝙤𝙢𝙚 𝙤𝙣𝙚 𝙙𝙖𝙮. Since 2021, I’ve invested in 𝗔𝗽𝗽𝗹𝗲, 𝗔𝗺𝗮𝘇𝗼𝗻, 𝗦𝗵𝗼𝗽𝗶𝗳𝘆, 𝗮𝗻𝗱 𝗚𝗼𝗼𝗴𝗹𝗲—working toward financial independence. I also look forward to being a 𝗹𝗼𝘃𝗶𝗻𝗴 𝗳𝗮𝘁𝗵𝗲𝗿 𝗮𝗻𝗱 𝗮 𝗱𝗲𝘃𝗼𝘁𝗲𝗱 𝗽𝗮𝗿𝘁𝗻𝗲𝗿, growing a 𝗺𝗶𝗹𝗹𝗶𝗼𝗻-𝗱𝗼𝗹𝗹𝗮𝗿 𝗯𝗿𝗮𝗻𝗱 together with my 𝗳𝘂𝘁𝘂𝗿𝗲 𝘄𝗶𝗳𝗲. Let’s connect and inspire each other on this journey!