JavaScript Object Properties
Learn how to access, modify, and manage object properties
🔑 What are Object Properties?
Properties are the values associated with a JavaScript object. They are name-value pairs that define the characteristics of an object.
let person = {
name: "John", // property
age: 30, // property
city: "Paris" // property
};
Accessing Properties
Dot Notation
Most common way to access properties
person.name
person.age
Bracket Notation
Useful for dynamic property names
person["name"]
person["age"]
Dynamic Access
Access properties using variables
let prop = "name";
person[prop]
Property Assignment
Add or modify properties
person.email = "[email protected]";
person["phone"] = "123-456-7890";
🔹 Dot vs Bracket Notation
Both methods access the same properties, but have different use cases:
let car = {
brand: "Honda",
model: "Civic",
year: 2022,
"fuel-type": "gasoline" // property with hyphen
};
// Dot notation (preferred for simple names)
console.log(car.brand); // "Honda"
console.log(car.model); // "Civic"
// Bracket notation (required for special characters)
console.log(car["fuel-type"]); // "gasoline"
console.log(car["year"]); // 2022
// Dynamic property access
let property = "brand";
console.log(car[property]); // "Honda"
Console Output:
Honda
Civic
gasoline
2022
Honda
🔹 Adding and Modifying Properties
You can add new properties or change existing ones:
let student = {
name: "Emma",
grade: "A"
};
// Add new properties
student.age = 19;
student.major = "Computer Science";
student["graduation-year"] = 2025;
// Modify existing properties
student.grade = "A+";
console.log(student);
// Output: {name: "Emma", grade: "A+", age: 19, major: "Computer Science", graduation-year: 2025}
Console Output:
{name: "Emma", grade: "A+", age: 19, major: "Computer Science", graduation-year: 2025}
🔹 Deleting Properties
Use the delete operator to remove properties:
let product = {
name: "Laptop",
price: 999,
category: "Electronics",
inStock: true
};
// Delete a property
delete product.inStock;
delete product["category"];
console.log(product);
// Output: {name: "Laptop", price: 999}
console.log(product.inStock); // undefined
Console Output:
{name: "Laptop", price: 999}
undefined
🔹 Checking if Properties Exist
Several ways to check if a property exists in an object:
let book = {
title: "JavaScript Basics",
author: "Jane Doe",
pages: 250
};
// Using 'in' operator
console.log("title" in book); // true
console.log("publisher" in book); // false
// Using hasOwnProperty()
console.log(book.hasOwnProperty("author")); // true
console.log(book.hasOwnProperty("isbn")); // false
// Checking if property is undefined
console.log(book.title !== undefined); // true
console.log(book.publisher !== undefined); // false
Console Output:
true
false
true
false
true
false
🔹 Looping Through Properties
Use for...in loop to iterate through object properties:
let animal = {
name: "Lion",
species: "Panthera leo",
habitat: "Savanna",
diet: "Carnivore"
};
// Loop through all properties
for (let property in animal) {
console.log(property + ": " + animal[property]);
}
// Get all property names
console.log(Object.keys(animal));
// Output: ["name", "species", "habitat", "diet"]
// Get all property values
console.log(Object.values(animal));
// Output: ["Lion", "Panthera leo", "Savanna", "Carnivore"]
Console Output:
name: Lion
species: Panthera leo
habitat: Savanna
diet: Carnivore
["name", "species", "habitat", "diet"]
["Lion", "Panthera leo", "Savanna", "Carnivore"]