?? Mastering JavaScript Objects: From Basics to Advanced Techniques! ??
JavaScript Developer WorldWide
Join the JavaScript Developers worldwide JavaScript Developers JavaScript Coders JavaScript Freelancers
Exercise 1: Creating an Object
Problem: Create a JavaScript object named book with properties title, author, and year.
Explanation: This exercise introduces creating a simple object in JavaScript, which is a collection of properties, with each property being a key-value pair.
Code:
let book = {
?title: "1984",
?author: "George Orwell",
?year: 1949
};
Exercise 2: Accessing Object Properties
Problem: Access the author property from the book object and print it to the console.
Explanation: Demonstrates two ways to access properties of an object: dot notation and bracket notation.
Code:
console.log(book.author); // Dot notation
console.log(book['author']); // Bracket notation
Exercise 3: Adding Properties
Problem: Add a new property genre with value "Dystopian" to the book object.
Explanation: Shows how to add new key-value pairs to an existing object.
Code:
book.genre = "Dystopian";
// Or
book['genre'] = "Dystopian";
Exercise 4: Deleting Properties
Problem: Remove the year property from the book object.
Explanation: Demonstrates how to delete a property from an object using the delete operator.
Code:
delete book.year;
Exercise 5: Looping through Object Properties
Problem: Use a for...in loop to print all properties and values of the book object.
Explanation: Shows how to iterate over the keys of an object and access their values.
Code:
for (let key in book) {
?console.log(key + ": " + book[key]);
}
Exercise 6: Checking for Property Existence
Problem: Check whether the book object has a property called author and print "Author exists" if true.
Explanation: Demonstrates how to check if an object has a specific property using the in operator or hasOwnProperty method.
Code:
if ('author' in book) {
?console.log("Author exists");
}
// Or
if (book.hasOwnProperty('author')) {
领英推荐
?console.log("Author exists");
}
Exercise 7: Nested Objects
Problem: Create a user object with properties name (a string), age (a number), and address (an object with properties street, city, and zipcode).
Explanation: Introduces objects within objects, showing how to structure complex data.
Code:
let user = {
?name: "Alice",
?age: 30,
?address: {
?street: "123 Maple St",
?city: "Springfield",
?zipcode: "12345"
?}
};
Exercise 8: Object Methods
Problem: Add a method fullName to the user object that takes firstName and lastName and sets a new fullName property on the user.
Explanation: Shows how to add methods (functions within an object) and use this to refer to the object itself.
Code:
user.fullName = function(firstName, lastName) {
?this.fullName = ${firstName} ${lastName};
};
user.fullName("John", "Doe");
console.log(user.fullName); // Outputs: John Doe
Exercise 9: Object Destructuring
Problem: Given an object employee with properties id, name, and department, use object destructuring to extract and print the name and department.
Explanation: Demonstrates the use of object destructuring, a convenient way to extract multiple properties from an object into variables.
Code:
let employee = {
?id: 1,
?name: "John Doe",
?department: "Finance"
};
let { name, department } = employee;
console.log(name); // John Doe
console.log(department); // Finance
Exercise 10: Merging Objects
Problem: Merge two objects, objectA with properties { a: 1, b: 2 } and objectB with properties { b: 3, c: 4 }, into a new object mergedObject.
Explanation: Explores object merging, particularly how properties from one object can override those of another.
Code:
let objectA = { a: 1, b: 2 };
let objectB = { b: 3, c: 4 };
let mergedObject = { ...objectA, ...objectB };
console.log(mergedObject); // { a: 1, b: 3, c: 4 }