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