Key-Value Data
Objects store data in key-value pairs, making them great for structured information.
They power most real-world data models in JavaScript.
- Use objects for named fields.
- Access values by key.
- Nest objects for deeper structures.
JavaScript Tutorial
Objects are the most common data structure in JavaScript for representing structured data.
They store information as key-value pairs and can include methods.
Most app data looks like objects: users, settings, products, and more.
Understanding objects is essential for working with real-world JavaScript code.
const obj = { key: value }
obj.key
obj['key']const user = {
name: "Ava",
age: 22,
};
console.log(user.name);Objects group related data under named keys.
const user = { name: "Ava" };
user.age = 22;
user.name = "Riya";
console.log(user);You can add or update properties anytime.
Objects store data in key-value pairs, making them great for structured information.
They power most real-world data models in JavaScript.
Properties store values and methods store functions.
Methods often use the this keyword to access other properties.
Use Object.keys, Object.values, and Object.entries to inspect objects.
Use Object.assign and spread to copy or merge objects.
Destructuring lets you unpack object properties into variables.
It reduces repetition and makes code cleaner.
const user = {
name: "Ava",
greet() {
return "Hi " + this.name;
}
};
console.log(user.greet());Methods are functions stored on objects.
const user = { name: "Ava", role: "admin" };
const { name, role } = user;
console.log(name, role);Destructure to avoid repeated user.name access.
Without
const name = user.name;
const role = user.role;With
const { name, role } = user;Use bracket notation for dynamic property names.
Clone with spread or Object.assign when needed.
Use this.property to access other fields inside methods.
A collection of key-value pairs.
Dot for static keys, bracket for dynamic keys.
Assign with obj.newKey = value or obj['newKey'] = value.
Practice: Create an object for a book with title and author, then log both.
// TODO: create book object
One Possible Solution
const book = { title: "Clean Code", author: "Robert Martin" };
console.log(book.title, book.author);Property order exists but should not be relied on for logic.
Use spread: const copy = { ...obj }.
A function stored as a property of an object.
Try adding and updating properties on the object.