Skip to content

Commit a9aa68a

Browse files
committed
[Add] Accessing objects using dot notation
1 parent 2da453e commit a9aa68a

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// 04_dot_notation.js
2+
3+
// Accessing Object Properties with Dot Notation
4+
/*
5+
In JavaScript, you can access object properties using dot notation.
6+
Dot notation is a convenient way to retrieve the value of a specific property from an object.
7+
*/
8+
9+
// Example: Accessing object properties with dot notation
10+
var student = {
11+
name: 'Alice',
12+
age: 25,
13+
grade: 'A'
14+
};
15+
16+
console.log(student.name); // Output: Alice
17+
console.log(student.age); // Output: 25
18+
console.log(student.grade); // Output: A
19+
20+
//Using Dot Notation to Call Object Methods
21+
22+
/*
23+
In addition to accessing properties, dot notation can also be used to call object methods.
24+
Methods are functions defined within an object that perform specific actions.
25+
*/
26+
27+
// Example: Using dot notation to call object methods
28+
var person = {
29+
firstName: 'John',
30+
lastName: 'Doe',
31+
fullName: function() {
32+
return this.firstName + ' ' + this.lastName;
33+
}
34+
};
35+
36+
console.log(person.fullName()); // Output: John Doe
37+
38+
// Section 7: Dot Notation for Nested Objects
39+
40+
/*
41+
You can also use dot notation to access properties of nested objects.
42+
Nested objects are objects that are properties of another object.
43+
*/
44+
45+
// Example: Accessing properties of nested objects with dot notation
46+
var library = {
47+
name: 'Local Library',
48+
address: {
49+
street: '123 Main St',
50+
city: 'Your town'
51+
}
52+
};
53+
54+
console.log(library.name); // Output: Local Library
55+
console.log(library.address.street); // Output: 123 Main St
56+
console.log(library.address.city); // Output: Anytown

0 commit comments

Comments
 (0)