Skip to content

JavaScript Objects

Introduction to JavaScript Objects

In JavaScript, objects are fundamental elements that help organize and store data. Imagine a JavaScript object as a special box where you can keep various items. Each item inside this box is stored in a small compartment labeled with a name (we call this the property name), and the item itself is the value. For example, if we have a box representing a book, it might have compartments labeled "title," "author," and "number of pages," with each compartment holding the respective information about the book.

Creating Objects

Using Object Literals

This is a straightforward method where you directly define the object and its properties using curly braces {}:


Output appears here after Run.

In this example, user is an object that has two properties: name and age. You can immediately see what the object contains, making this method very transparent and easy to use.

Using the new Object() Syntax

Alternatively, you can start with an empty object and then add properties one by one:


Output appears here after Run.

This method involves first creating an empty object (user) and then adding properties (name and age). It's useful when the properties of the object depend on conditional logic or when the properties are determined at runtime.

Why Are Objects Important?

Now that we've introduced JavaScript objects, you might wonder about their practical importance. Let's explore why mastering JavaScript objects is essential for anyone beginning their journey in web development.

  1. Organization: Objects help you keep related data together, making your code cleaner and easier to understand. Instead of having many separate variables, you can group related data into a single object.

Output appears here after Run.

This book object keeps all related information about the book neatly organized under one variable.

  1. Reusability: Once you create an object, you can reuse it throughout your code. This simplifies the management of common data and functionality.

Output appears here after Run.

The user object can be passed to different functions like displayUserInfo without needing to redefine the user's information.

  1. Flexibility: Objects in JavaScript are very flexible. You can add new properties, change values of existing properties, or delete properties as needed.

Output appears here after Run.
  1. Functionality: Objects can also contain functions. These functions (known as methods) can perform actions using the data held in the object.

Output appears here after Run.

The person object has a greeting method that utilizes its own data to produce a greeting. You may wonder about the this keyword in this example, which you can find more information on our JavaScript this keyword article.

  1. Prototypes: Every JavaScript object has a prototype. A prototype is a template object from which the object inherits methods and properties. This allows for powerful inheritance models.

Output appears here after Run.

Here, dog inherits from animal, so it has access to the isAlive property defined in animal, plus its own bark method.

Accessing and Modifying Properties

Objects allow you to access and modify their properties using dot notation or bracket notation.

Accessing Properties


Output appears here after Run.

user.name accesses the name property using dot notation, which is straightforward and easy to read. user["age"] uses bracket notation, beneficial when the property name is stored in a variable or is not a valid identifier.

You can also retrieve all keys or values of an object using Object.keys() and Object.values():


Output appears here after Run.

These methods are useful for iterating over object properties or converting them to arrays.

Modifying Properties

You can also change the properties of an object or add new ones:


Output appears here after Run.

The example shows adding a new boolean property (isAdmin), changing the value of an existing property (name), and deleting a property (age). This demonstrates the mutable nature of JavaScript objects.

Let vs Const with Objects

Let


Output appears here after Run.

Using let allows reassignment of the object to a new object. This flexibility is useful when the entire object needs to be replaced.

Const


Output appears here after Run.

With const, you can modify the properties of the object but cannot assign a new object to the variable. This ensures that the reference to the object remains constant.

Object Destructuring and Rest Operator

Destructuring

Destructuring allows you to unpack properties from an object into separate variables:


Output appears here after Run.

This syntax extracts the firstName, lastName, and age properties from the person object into individual variables, simplifying access to these properties.

Rest Operator

The rest operator (...) is used to gather the remaining properties that have not been unpacked:


Output appears here after Run.

After extracting age, the rest operator collects the remaining properties into a new object nameDetails, which includes only firstName and lastName.

Advanced Property Attributes and Methods

Getters and Setters

Getters and setters are functions that control how you access and set properties of an object:


Output appears here after Run.

The fullName property is controlled by a getter and a setter. The getter returns the full name by combining firstName and lastName. The setter takes a full name, splits it into two parts, and assigns them to firstName and lastName, respectively.

Conclusion

JavaScript objects provide a structured, efficient, and powerful way to manage and manipulate data, which is essential for creating interactive and dynamic web applications. Understanding how to effectively utilize objects is crucial for anyone looking to advance their JavaScript skills.

Practice

What are the ways to create an object in JavaScript?

Dual-run preview — compare with live Symfony routes.