Objects and Arrays
Revisit your understanding of objects and arrays, essential tools for structuring and manipulating data in JavaScript.
We'll cover the following...
Objects and arrays are fundamental building blocks in JavaScript. Objects allow us to structure data using key-value pairs, making it easy to represent entities like users, products, or books. Arrays, on the other hand, are perfect for organizing lists of data that need to be iterated or transformed.
Working with objects
Objects in JavaScript are collections of key-value pairs, where keys are strings (or symbols) and values can be any data type, including other objects or functions. This flexibility makes objects a powerful tool for representing real-world entities and organizing complex, hierarchical data. They form the backbone of JavaScript's structure, enabling features like dynamic property access, method invocation, and seamless integration with JSON for data exchange.
Creating and accessing objects
We can create objects using curly braces {}
and access their properties using dot notation or bracket notation.
// Creating an objectconst user = {name: "Alice",age: 25,isAdmin: false};// Accessing propertiesconsole.log(user.name); // Aliceconsole.log(user["age"]); // 25
Modifying objects
We can add, update, or delete properties in an object.
// Adding a propertyuser.email = "alice@example.com";// Updating a propertyuser.age = 26;// Deleting a propertydelete user.isAdmin;console.log(user);
Iterating through objects
We use for...in
loop to iterate over an object’s ...