JavaScript Object Properties

Back to JS page

Table of Contents

Accessing Properties

JavaScript object properties can be accessed in three ways:

<!DOCTYPE html>
<html>
<body>

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

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

<script>
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 30,
  "home town": "New York"
};

let text = "Dot notation: " + person.firstName + "<br>";
text += "Bracket notation: " + person["lastName"] + "<br>";
text += "Property with space: " + person["home town"] + "<br>";

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

</body>
</html>

Example 1

Result View Example

Adding New Properties

You can add new properties to an existing object by simply giving it a value:

<!DOCTYPE html>
<html>
<body>

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

<h4>Adding New Properties</h4>
<p id="demo"></p>

<script>
const person = { firstName: "John", lastName: "Doe" };

// Add new properties
person.age = 30;
person["eyeColor"] = "blue";
person["favorite color"] = "green";

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

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

</body>
</html>

Example 2

Result View Example

Deleting Properties

The delete keyword deletes a property from an object:

<!DOCTYPE html>
<html>
<body>

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

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

<script>
const car = {
  brand: "Toyota",
  model: "Corolla",
  year: 2020,
  color: "blue",
  price: 25000
};

let text = "Before delete:<br>";
for (let key in car) {
  text += key + ": " + car[key] + "<br>";
}

// Delete the price property
delete car.price;
// Delete using bracket notation
delete car["color"];

text += "<br>After delete (price, color):<br>";
for (let key in car) {
  text += key + ": " + car[key] + "<br>";
}

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

</body>
</html>

Example 3

Result View Example

Nested Properties

Properties can contain other objects, creating nested structures:

<!DOCTYPE html>
<html>
<body>

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

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

<script>
const company = {
  name: "Tech Corp",
  address: {
    street: "456 Oak Ave",
    city: "San Francisco",
    state: "CA"
  },
  employees: {
    manager: { name: "Alice", salary: 80000 },
    developer: { name: "Bob", salary: 65000 }
  }
};

// Access nested properties
let text = "Company: " + company.name + "<br>";
text += "City: " + company.address.city + "<br>";
text += "Manager: " + company.employees.manager.name + "<br>";
text += "Developer salary: $" + company.employees.developer.salary;

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

</body>
</html>

Example 4

Result View Example

Using Variables as Property Names

You can use variables to access object properties dynamically:

<!DOCTYPE html>
<html>
<body>

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

<h4>Using Variables as Property Names</h4>
<p id="demo"></p>

<script>
const person = {
  name: "John",
  age: 30,
  city: "New York",
  country: "USA"
};

const keys = ["name", "age", "city", "country"];

let text = "Dynamic property access:<br><br>";
for (let i = 0; i < keys.length; i++) {
  const key = keys[i];
  text += key + ": " + person[key] + "<br>";
}

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

</body>
</html>

Example 5

Result View Example

Document

Document in project

You can Download PDF file.

Reference