Skip to content

Commit 669aee5

Browse files
authored
Update OptionalChaining.js
1 parent 62156f2 commit 669aee5

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

OptionalChaining.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@ const user = {
55
street:" Road ",
66
city : "Patna" ,
77
state : "Bihar",
8-
zip : 4657890
9-
10-
}
11-
8+
zip : 4657890
9+
}
1210
}
13-
1411
console.log(user.address.country?.pin); //undefined
12+
13+
//Optional chaining can be used to acess the elements of the array in JavaScript . It works in a similar way to acessing properties
14+
// of an object but using ?.[] instead of the ?. operator
15+
16+
const users = [
17+
{name : "Riya" , age: 20 },
18+
{
19+
name : "bebe" , age : 25},
20+
{
21+
name : "jenny", age : 30} ,
22+
];
23+
24+
console.log(users[3]?.age);
25+
26+
27+
28+

0 commit comments

Comments
 (0)