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: 09-Data-Structure-Modern-Operators-and-Strings/README.md
+73-1Lines changed: 73 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,7 +21,7 @@ console.log(...data);
21
21
22
22
Another use case of the spread operator is for passing in multiple values into a function.
23
23
24
-
# Rest Patter and Parameters
24
+
# Rest Pattern and Parameters
25
25
26
26
The rest pattern is the opposite of the spread operator. It uses the exact same syntax, however, it it used to collect multiple elements and condense them into an array, whereas, the spread operator is used for unpacking an array into multiple elements or array.
27
27
@@ -94,3 +94,75 @@ The subtle distinction that tells you when and where to use spread and rest.
94
94
95
95
- Use Spread when you want to write values seperated by commas.
96
96
- Use Rest when you want to write variable names seperated by commas.
97
+
98
+
# Short Circuting (&& and ||)
99
+
100
+
In the past, we've looked at the `&&` and `||` operators but we only crossed the surface. In this section, we'll look at how to use these operators for something called circuting.
101
+
102
+
### Properties of logical operators
103
+
104
+
1. They can use any data type
105
+
2. They can return any data type
106
+
3. They can perform short circuting (short circuit evaluation)
107
+
108
+
### Short Circuting Or ||
109
+
110
+
For example:
111
+
112
+
```js
113
+
console.log(3||"Eke");
114
+
```
115
+
116
+
In the case of the OR operator, short circuting means that if the first operand is a truthy value, it will return that value and not even evaluate the other value.
117
+
118
+
```js
119
+
console.log("🔸Short Circuting🔸");
120
+
console.log(3||"Eke"); // Truthy
121
+
console.log(""||"Eke"); // Falsy
122
+
console.log(true||0); // Truthy
123
+
console.log(undefined||null); // Falsy
124
+
console.log(undefined||0||""||"Hello"||23||null);
125
+
```
126
+
127
+
From the example above, we can see that the result of the OR operator doesn't have to be a boolean. It will simply short circut the entire operation and return the first truthy value.
// In the example above before numGuest was assigned the number 23, restaurant.numGuest was undefined, which makes it a falsy value. And as such the operand will skip to the next value which is 10.
135
+
136
+
// Instead of using a tenary operator, we can use short0-circuting in the OR operator.
137
+
138
+
constguest2=restaurant.numGuests||10;
139
+
console.log(guest2);
140
+
```
141
+
142
+
### Short Circuting And &&
143
+
144
+
Short circuting the And operator works exactly the opposite of the OR operator. This means that if the first operand is a falsy value, it will return that value and not evaluate the other operand.
- The OR operator will return the first truthy value of all the operands or simply return the last value if all the operands are falsy.
165
+
166
+
- The AND operator will return the first falsy value or the last value if all the operands are truthy.
167
+
168
+
In practical real world examples, the OR operator can be used for setting default values and the AND operator for setting executing code in the second operand if the first operand is true.
0 commit comments