You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/javascript/concepts/optional-chaining/optional-chaining.md
+52-32Lines changed: 52 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ CatalogContent:
14
14
15
15
The **optional chaining** operator simplifies comparing multiple data properties in a chain of connected objects. This is especially valuable if any of the properties are `null`, as the operator will return `undefined` instead of throwing an error.
16
16
17
-
Optional chaning can be used to validate nested object properties, call optional functions or methods on optional objects, accessing arrary elements, and more.
17
+
Optional chaning can be used to validate nested object properties, call optional functions or methods on optional objects, accessing array elements, and more.
18
18
19
19
The primary benefits of using optional chaining instead of regular syntax include simplifying the code to achieve the same result, making it easier to read, protecting against runtime errors, and enhancing maintainability.
20
20
@@ -33,8 +33,10 @@ array?.[index]
33
33
object?.method?.()
34
34
```
35
35
36
-
## Example
37
-
This person object lists an individual's details and containing properties, nested objects, an array, and a method. Optional chaining for each conditional evaluation is listed below.
36
+
## Examples
37
+
38
+
### Accessing Object Properties
39
+
To search for the `state` object of `person`:
38
40
39
41
```js
40
42
constperson= {
@@ -44,48 +46,66 @@ const person = {
44
46
address: {
45
47
street:"1111 Palm Ave",
46
48
city:"Broken Arrow",
47
-
state:"Oklahoma"
49
+
state1:"Oklahoma"
48
50
},
49
51
favoriteFoods: ["pizza", "ice cream", "cake"],
50
52
commonPhrase:function() {
51
-
return`${this.name} always says "I love ${this.address.state}."`;
53
+
return`${this.name} always says "I love ${this.address.state1}."`;
52
54
}
53
55
};
54
-
```
55
56
56
-
## Accessing Object Properties
57
-
To search for the `state` object of `person`:
57
+
// Regular syntax for checking if the state1 property exists
58
+
conststate1Regular= person &&person.address&&person.address.state1;
59
+
console.log(`State (regular syntax) is: ${state1Regular}`);
58
60
59
-
```js
60
-
//Regular syntax
61
-
conststate= person &&person.address&&person.address.state;
62
-
console.log(`State is: ${state}`);
61
+
// This can be rewritten as:
62
+
conststate1Chaining= person?.address?.state1;
63
+
console.log(`State (optional chaining) is: ${state1Chaining}`);
64
+
```
63
65
64
-
//This can be rewritten as:
65
-
conststate= person?.address?.state;
66
-
console.log(`State is: ${state}`);
66
+
The output of the above code will be as follows:
67
67
68
-
//The output for both is:
69
-
State is: Oklahoma
68
+
```shell
69
+
State (regular syntax) is: Oklahoma
70
+
State (optional chaining) is: Oklahoma
70
71
```
71
72
72
-
## Accessing Arrary Elements
73
+
### Accessing Array Elements
73
74
To search for the `cake` in the `favoriteFoods` array of `person`:
74
75
75
76
```js
76
-
//Regular Syntax
77
-
constfood= person &&person.favoriteFoods&&person.favoriteFoods.find(item=> item ==="cake");
78
-
console.log(`Favorite Food is: ${food}`);
77
+
constperson= {
78
+
name:"Alice",
79
+
gender:"Female",
80
+
age:12,
81
+
address: {
82
+
street:"1111 Palm Ave",
83
+
city:"Broken Arrow",
84
+
state1:"Oklahoma"
85
+
},
86
+
favoriteFoods: ["pizza", "ice cream", "cake"],
87
+
commonPhrase:function() {
88
+
return`${this.name} always says "I love ${this.address.state1}."`;
0 commit comments