By the end of this lesson, you will understand how to store related information in objects.
Objects in JavaScript are a way to group related data together. For example, if you want to store information about a book, you can use an object. This object can hold data like title, author, and year of publication.
As an example, let’s create an object to represent a book.
File: book.js
const book = {
title: 'To Kill a Mockingbird',
author: 'Harper Lee',
year: 1960
};
console.log(book.title);
console.log(book.author);
console.log(book.year);
To Kill a Mockingbird
Harper Lee
1960
book.title, author, and year.console.log() to print each property to the console.Create an object that represents your favorite movie. Include properties like title, director, and year released. Log all properties to the console.
const favoriteMovie = {
title: 'Inception',
director: 'Christopher Nolan',
year: 2010
};
console.log(favoriteMovie.title);
console.log(favoriteMovie.director);
console.log(favoriteMovie.year);
{}. Using square brackets [] will create an array instead of an object.