...

/

Accessing Object Properties

Accessing Object Properties

This lesson discusses the different ways to access properties, such as using dot operator and square brackets.

There are various ways to access object properties. We will discuss each method in detail below.

Dot Operator

In JavaScript, an object literal can be accessed using the dot operator. To access any property, the name of the object should be mentioned first, followed by the dot operator and then the name of the property encapsulated in that object.

Example

Let’s take a look at an example:

Press + to interact
//creating an object named shape
var shape = {
//defining properties of the object
//setting data values
name : 'square',
sides : 4
}
//accessing the properties using the dot operator
console.log("Name is:", shape.name) //using dot operator to access "name"
console.log("Number of sides are:", shape.sides) //using dot operator to access "sides"
...