JavaScript Objects

What are JavaScript Objects?

JavaScript objects are essential for organizing and structuring data. They allow developers to group related properties and methods into a single entity, simplifying the management of complex data. In this guide, we’ll cover how to create, modify, and work with JavaScript objects in your programs.

Importance of Objects in JavaScript

Objects are a core component of JavaScript, grouping data and functionalities together. They play a key role in interacting with HTML elements through the DOM, managing arrays, and structuring more complex data models.

For example, a car object might have properties like make, model, and color, and methods to start, stop, or accelerate. This approach helps developers manage and manipulate data more efficiently.

Object Properties and Notations

Objects contain properties, which are characteristics of the object, such as color, size, or type. There are two common ways to access these properties: dot notation and bracket notation.

Understanding Object Properties

In modern JavaScript (ECMAScript 2015 and later), the iteration order of an object's properties is guaranteed to be the same as when they were added, simplifying how developers interact with objects.

Using Dot Notation to Access Object Properties

Dot notation allows you to access an object’s properties by simply using the object’s name followed by a dot and the property name:

let person = {firstName: "John", lastName: "Doe"};
console.log(person.firstName); // Outputs: John 

Using Bracket Notation to Access Object Properties

Bracket notation is useful when the property name is stored in a variable or contains special characters:

let person = {name: "John", age: 30};
let prop = "name";
console.log(person[prop]); // Outputs: John

Creating and Defining JavaScript Objects

There are multiple ways to create objects in JavaScript, including object literal syntax and constructor functions.

Object Literal Syntax

This is the most straightforward way to create an object:

let car = {brand: "Toyota", model: "Camry", year: 2022};

You can easily add more properties:

let car = {brand: "Toyota", model: "Camry", year: 2022, color: "silver", price: 25000};

Key-Value Pairs in Object Literal Syntax

Objects consist of key-value pairs, which are written within curly braces and separated by commas:

let person = {name: "John Doe", age: 30, gender: "male"};

You can also nest objects within objects:

let person = {
  name: "John Doe",
  age: 30,
  address: {
    street: "123 Main St",
    city: "Anytown"
  }
};

Creating a Person Object with Multiple Properties

You can create an object using either the object initializer or a constructor function:

let person = {name: "John Doe", age: 30, isAdmin: false};

Or, using a constructor function:

function Person(name, age, isAdmin) {
  this.name = name;
  this.age = age;
  this.isAdmin = isAdmin;
}
let person = new Person("John Doe", 30, false);

Working with Object Methods

Object methods are functions that perform actions on object data. You define methods by adding a function as a property of the object:

let car = {
  brand: "Toyota",
  startEngine: function() {
    console.log(this.brand + " engine started");
  }
};
car.startEngine(); // Outputs: Toyota engine started

Using Methods to Manipulate Objects

Methods allow you to change object properties or perform actions on the object:

let car = {
  brand: "Toyota",
  startEngine: function() {
    console.log(this.brand + " engine started");
  }
};
car.startEngine(); // Outputs: Toyota engine started

Managing Property Values in Objects

Setting Property Values

You can set object properties dynamically using dot notation:

let person = {name: "John"};
person.age = 30;

Retrieving Property Values

To get a property value, simply use dot or bracket notation:

console.log(person.name); // Outputs: John

Understanding Primitive Data Types in JavaScript

JavaScript includes several primitive data types:

  • Boolean: Represents true or false.
  • Number: Represents numeric values.
  • String: Represents text.
  • Null: Represents an intentional absence of value.
  • Undefined: Represents a variable that has been declared but not yet assigned a value.
  • Symbol: Represents a unique and immutable value.
  • BigInt: Allows the representation of integers larger than the Number data type can handle.

Understanding these data types is essential for working with variables and performing various operations in JavaScript.

Create a free account to access the full topic

“It has all the necessary theory, lots of practice, and projects of different levels. I haven't skipped any of the 3000+ coding exercises.”
Andrei Maftei
Hyperskill Graduate