🧠 JavaScript Objects and their Manipulations
📦 Object Creation
An example of a user-related simple object is shown below.
const user = { name: "Alice", age: 25, email: "alice@example.com" };
🔍 Accessing Values of an Object’s Property
You can access values by the property name through the dot or the brackets notation.
console.log(user.name); // Alice
console.log(user["email"]); // alice@example.com
📝 Modifying An Object's Field
To make a change to the object, assign a different value to that key.
user.age = 26;
user["email"] = "alice.new@example.com";
➕ Property Addition
It is also possible to add properties dynamically.
user.country = "India";
❌ Object Property Removal
To remove a given property, employ the delete operator.
delete user.age;
🔁 Object Key Iteration
A for…in loop will iterate all keys.
for (let key in user) {
console.log(key + ": " + user[key]);
}
✅ TL;DR: Object Cheatsheet
- To create:
const obj = { key : value }
- To access:
obj.key
orobj[“key”]
- To update:
obj.key = new Value
- To add:
obj.newKey = value
- To delete:
delete obj.key
- To loop:
for…in
for iterating in the keys or values.
💡 Pro Tip: Managing data that is structured like users, their settings, or any other containing multi-values, would be easier with object-oriented programming.