JavaScript Object Properties
Subject: JavaScript
In JavaScript, object properties define the structure and data stored in an object. Each property is a key-value pair, where the key identifies the property and the value holds the associated data.
Understanding Object Properties
A property has two parts:
- Key: A string or symbol used as the property name
- Value: The data associated with that key
Types of Property Keys
- String (default): "name", "age"
- Symbol: A unique identifier
Accessing Properties
Dot Notation:
Bracket Notation:
Useful for dynamic or complex keys:
Adding and Updating Properties
Deleting Properties
Property Existence Check
Looping Through Properties
Object.defineProperty()
Use this method to define a property with fine-grained control:
Computed Property Names
Create dynamic property keys:
Property Shorthand (ES6)
When the variable name and property name match:
Key Takeaways
- Object properties store data as key-value pairs.
- Use dot or bracket notation to access or modify values.
- Check existence using
inorhasOwnProperty(). - Use
Object.defineProperty()for advanced control. - ES6 provides shorthand and computed property name features for cleaner code.