SmartCodingTips

JavaScript Objects and Properties

Objects in JavaScript are collections of key-value pairs. They are used to store structured data and complex entities.

📦 Creating Objects

// Object literal
const user = {
    name: "Alice",
    age: 25,
    isStudent: true
};

// Using Object constructor
const person = new Object();
person.name = "Bob";
person.age = 30;

🔍 Accessing Properties

console.log(user.name);     // Dot notation
console.log(user["age"]);   // Bracket notation

let key = "isStudent";
console.log(user[key]);     // Dynamic key access

🛠️ Modifying and Deleting

user.age = 26;              // Modify
user.city = "New York";     // Add new property
delete user.isStudent;      // Delete property

🔁 Looping Through Properties

for (let key in user) {
    console.log(key + ": " + user[key]);
}

✅ Property Existence

console.log("name" in user);    // true
console.log(user.hasOwnProperty("age")); // true
💡 Tip: Object keys are always strings or symbols. Values can be any data type, including functions (methods).