JavaScript Object Properties

In JavaScript the members inside the object which are the key: values are called Object properties. For example, in the above-given syntax key_1: value_1 and key_2: value_2 are properties of the object. 

To Access Object Properties:

1. Using dot Notation: 

Syntax:

object_name.key_1

2. Using bracket Notation: 

Syntax:

object_name["key_1"]

JavaScript Nested Objects: In this case, an object contains another object inside it. 

Syntax:

const object_name-1 = {
key_1: value_1,
key_2: value_2,
const object_name-2 = {
key_3: value_3,
key_4: value_4,
}
}

JavaScript Object Methods: In JavaScript, we can add methods to Objects.

Syntax:

const object_name-1 = {
method_name: function
() {
//code here
}
}

Example: In the given example we can see how we can apply Javascript nested objects and also use the different accessing methods.

Javascript




const GFG = {
    topic: 'OOPs',
    lang: 'JavaScript',
    sub_topics: {
        topic_1: 'Class',
        topic_2: 'Object'
    }
}
console.log(GFG.topic);
console.log(GFG.sub_topics.topic_1);
console.log(GFG["lang"]);
console.log(GFG.sub_topics["topic_2"]);


Output:

OOPs
Class
JavaScript
Object


Classes and Objects in JavaScript

Similar Reads

Classes

Classes were first introduced in the new version of the ES6 classes which replaced the previously used functions. Class is nothing but a blueprint for an object of it. It is used to create an object mainly. If we relate it to a real-life example then it is like a plan for a building or house where that plan contains details about doors, windows, floors, etc. Based on the class which is the blueprint an object is made which can be referred to as a house in this example. So one plan is used to make a lot of houses in the same way one class can be used to create a lot of classes. So class is not a real-life entity but the object is one....

Object

...

JavaScript Object Properties

Apart from the 7 primitive datatypes there is the most important datatype in JavaScript which is Object. Although the nature of this datatype is completely different from the primitive datatypes. That means in the primitive datatypes only one value is stored but an object can store more than one value even of different types....