Creating Objects

Objects Overview

Creating Objects

🙋 Need help? Ask an expert now!

JavaScript object literal is a simplified way to create objects using curly brackets. The properties of the object belong inside the curly brackets, each with a name and a value. These properties can be of any data type, including a function. The properties can then be accessed using the dot notation just like how you would access the elements of an array.

The object above has three properties, the strings firstName, lastName, and the function welcomeMessage. These properties belong to the object and can only be accessed using dot notation. As shown in the console.log() statement, the properties are accessed one by one and printed to the console. This values inside the objects are not affected by the dot notation and cannot be changed from the outside.

Edit Me on GitHub!

Application Question

Consider the following code segment:

let person = {
    firstName: 'Hollis',
    lastName: 'Brown',
    welcomeMessage: function () {
        return 'Welcome ' + this.firstName + ' ' + this.lastName + '!';
    }
};
person.welcomeMessage();

What will the following code segment print to the console?