Properties have a **name** (string or Symbol) and a **value** (any type). Access them with **dot notation** (for valid identifiers) or **bracket notation** (for dynamic keys or special chars). Use `in` operator or `hasOwnProperty` to check existence. `delete` removes properties.
1Understanding Object Properties
Properties have a name (string or Symbol) and a value (any type). Access them with dot notation (for valid identifiers) or bracket notation (for dynamic keys or special chars). Use in operator or hasOwnProperty to check existence. delete removes properties.
Bracket notation allows dynamic keys: obj[variableName]. Dot notation is cleaner but only works for valid identifier names.
const car = { make: 'Toyota', model: 'Camry', year: 2022 };
// Dot notation
console.log(car.make); // Toyota
// Bracket notation (dynamic key)
const prop = 'model';
console.log(car[prop]); // Camry
// Check existence
console.log('year' in car); // true
console.log(car.hasOwnProperty('model')); // true2Practical Example
Here is a real-world application of Object Properties showing how it is used in production JavaScript code.
// Add, modify, delete properties
const user = { name: 'Alice' };
user.email = 'alice@example.com'; // add
user.name = 'Alice Smith'; // modify
delete user.email; // delete
console.log(user);3Best Practices
Follow these guidelines when working with Object Properties:
1. Use dot notation by default
2. Use bracket notation for dynamic keys
3. Check property existence with 'key in obj' or obj.hasOwnProperty(key)
Tip: Bracket notation allows dynamic keys: obj[variableName]. Dot notation is cleaner but only works for valid identifier names.
const car = { make: 'Toyota', model: 'Camry', year: 2022 };
// Dot notation
console.log(car.make); // Toyota
// Bracket notation (dynamic key)
const prop = 'model';
console.log(car[prop]); // Camry
// Check existence
console.log('year' in car); // true
console.log(car.hasOwnProperty('model')); // true