JavaScript / JavaScript Arrays and Objects

Working with JavaScript Objects

In this tutorial, we will delve into JavaScript objects, a crucial aspect of JavaScript. You will learn how to create, access, and manipulate objects.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Explains how to work with arrays and objects to store and manipulate data effectively.

Working with JavaScript Objects

1. Introduction

In this tutorial, we aim to provide a comprehensive guide to understanding JavaScript objects. We will learn how to create, access, and manipulate objects in JavaScript.

By the end of this tutorial, you should be able to:
- Understand what JavaScript objects are
- Create your own objects
- Access and modify properties of an object
- Use methods with objects

Prerequisites: Basic understanding of JavaScript syntax and data types.

2. Step-by-Step Guide

Understanding JavaScript Objects

JavaScript objects are containers for named values, called properties and methods. The properties of an object define the characteristics of the object, and the methods define the actions the object can perform.

Creating a JavaScript Object

You can define (and create) a JavaScript object with an object literal:

let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};

In the example above, 'person' is an object. The object has four properties: firstName, lastName, age and eyeColor.

Accessing Object Properties

You can access object properties in two ways:

  1. ObjectName.PropertyName
  2. ObjectName["PropertyName"]
console.log(person.firstName); // Output: "John"
console.log(person["lastName"]); // Output: "Doe"

Changing Object Properties

You can change the value of an object property using the assignment operator:

person.firstName = "Jane"; // changes the firstName property to "Jane"

JavaScript Object Methods

Methods are actions that can be performed on objects. Methods are stored in properties as function definitions.

let person = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

In the example above, fullName is a method, and it will return "John Doe" when called: person.fullName().

3. Code Examples

Creating a JavaScript Object

let car = {
  make: "Toyota",
  model: "Corolla",
  year: 2020,
  color: "red"
};

console.log(car); // Output: { make: 'Toyota', model: 'Corolla', year: 2020, color: 'red' }

Accessing and Modifying Object Properties

console.log(car.make); // Output: "Toyota"
console.log(car["model"]); // Output: "Corolla"

car.year = 2021; // changes the year property to 2021
console.log(car.year); // Output: 2021

Using Object Methods

let person = {
  firstName: "John",
  lastName : "Doe",
  id       : 5566,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

console.log(person.fullName()); // Output: "John Doe"

4. Summary

In this tutorial, we've learned what JavaScript objects are, how to create them, and how to access and modify their properties. We've also learned how to use methods with objects.

The next step in your learning journey could be learning about JavaScript Object Prototypes, or perhaps exploring more about functions and how they work with objects.

Additional resources:
1. Mozilla Developer Network - JavaScript objects
2. W3Schools - JavaScript Objects

5. Practice Exercises

  1. Create a JavaScript object representing a book with properties like title, author, year of publication, and a method that returns a summary of the book.
  2. Modify the properties of the book object you created, and then print the updated properties.
  3. Add a new method to the book object that returns the age of the book based on the current year.

Solutions with explanations and tips for further practice can be found in the additional resources provided.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

MD5/SHA Hash Generator

Generate MD5, SHA-1, SHA-256, or SHA-512 hashes.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help