JavaScript Object Introduction

Back to JS page

Table of Contents

What Are Objects?

JavaScript objects are containers for named values called properties.

Objects are variables too. But objects can contain many values.

This code assigns many values (firstName, lastName, age) to a variable named person:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Introduction</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>What Are Objects?</h4>
<p id="demo"></p>

<script>
// Create an object
const car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2020,
  color: "blue"
};

// Display object properties
document.getElementById("demo").innerHTML =
  "Car: " + car.brand + " " + car.model + " (" + car.year + ")";
</script>

</body>
</html>

Example 1

Result View Example

Object Properties

Properties are the most important part of JavaScript objects. Properties can be accessed, added, changed, and removed:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Introduction</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Object Properties</h4>
<p id="demo"></p>

<script>
const student = {
  name: "Alice",
  grade: "A",
  subject: "Mathematics"
};

// Access properties using different methods
let text = "Student: " + student.name + "<br>";
text += "Grade: " + student["grade"] + "<br>";
text += "Subject: " + student["subject"] + "<br><br>";

// List all properties
text += "All properties: " + Object.keys(student).join(", ") + "<br>";
text += "Total properties: " + Object.keys(student).length;

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 2

Result View Example

Adding and Deleting Properties

You can add new properties to an object and delete existing ones:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Introduction</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Adding and Deleting Properties</h4>
<p id="demo"></p>

<script>
const book = {
  title: "JavaScript Guide",
  author: "John Doe",
  pages: 500
};

let text = "Original properties:<br>";
for (let key in book) {
  text += key + ": " + book[key] + "<br>";
}

// Add a new property
book.publisher = "Tech Books Inc.";
book.year = 2024;

text += "<br>After adding properties:<br>";
for (let key in book) {
  text += key + ": " + book[key] + "<br>";
}

// Delete a property
delete book.pages;

text += "<br>After deleting 'pages':<br>";
for (let key in book) {
  text += key + ": " + book[key] + "<br>";
}

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 3

Result View Example

Nested Objects

Property values can be other objects, creating nested objects:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Introduction</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Nested Objects</h4>
<p id="demo"></p>

<script>
const user = {
  id: 1001,
  name: "John Doe",
  contact: {
    email: "john@example.com",
    phone: "555-1234",
    address: {
      street: "123 Main St",
      city: "New York",
      zip: "10001"
    }
  }
};

// Access nested properties
document.getElementById("demo").innerHTML =
  "Name: " + user.name + "<br>" +
  "Email: " + user.contact.email + "<br>" +
  "Phone: " + user.contact.phone + "<br>" +
  "City: " + user.contact.address.city + "<br>" +
  "ZIP: " + user.contact.address.zip;
</script>

</body>
</html>

Example 4

Result View Example

Iterating Over Properties

You can loop through all properties of an object using for...in:

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Object Introduction</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>

<h4>Iterating Over Properties</h4>
<p id="demo"></p>

<script>
const fruit = {
  name: "Apple",
  color: "Red",
  taste: "Sweet",
  origin: "China",
  price: 1.25
};

let text = "<ul>";
for (let key in fruit) {
  text += "<li>" + key + ": " + fruit[key] + "</li>";
}
text += "</ul>";

// Count properties
text += "Total properties: " + Object.keys(fruit).length;

document.getElementById("demo").innerHTML = text;
</script>

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference