-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptionalChaining.js
More file actions
67 lines (57 loc) · 2.27 KB
/
optionalChaining.js
File metadata and controls
67 lines (57 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
* Optional chaining, represented by ?. in JavaScript, it is a new feature introduced in ES2020. Optional chaining changes the way properties are accessed from deeply nested objects.
* It fixes the problem of having to do multiple null checks when accessing a long chain of object properties in JavaScript
*/
const user = {
name: 'yamini',
// address: {
// city: 'tenali'
// }
}
console.log(user.address); // {city: 'tenali'}
//console.log(user.address.city); // tenali // If i comment down the address object then it will return TypeError(: Cannot read property 'city' of undefined)
//console.log(user.address ? user.address.city : undefined); // tenali // If i comment down the address object then it will return undefined
console.log(user.address && user.address.city); // undefined
let key = "accountToken";
const customerDetail = {
userId: '12345',
account: {
// accountToken: 'ABC1234',
holder: {
profile: {
firstName: 'yamini',
lastName: 'kota',
birthDate: '24-08-1996'
}
},
contacts: {
address: {
city: 'Delhi',
state: 'Delhi',
countryName: 'India'
},
phone: [
{
type: 'Home',
phoneNumber: '5354646456'
},
{
type: 'Work',
phoneNumber: '7868765490'
}
]
}
}
}
console.log('accountToken>>>>>', customerDetail.account[key]);
console.log(customerDetail.account.holder.profile.birthDate); // If i comment down the address object then it will return undefined
console.log(customerDetail.account &&
customerDetail.account.holder &&
customerDetail.account.holder.profile &&
customerDetail.account.holder.profile.birthDate);
/**
* The optional chaining ?. is a safe way to access nested object properties, even if an intermediate property doesn't exist.
* The optional chaining ?. stops the evaluation if the value before ?. is undefined or null and resturns undefined.
*/
/** We can also stop the execution of a function execution using optional chaining*/
console.log(customerDetail.getDetail?.());