Skip to content

Commit b3470ac

Browse files
committed
complete short circuting
1 parent 9a8b3d8 commit b3470ac

File tree

3 files changed

+101
-2
lines changed

3 files changed

+101
-2
lines changed

09-Data-Structure-Modern-Operators-and-Strings/README.md

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ console.log(...data);
2121

2222
Another use case of the spread operator is for passing in multiple values into a function.
2323

24-
# Rest Patter and Parameters
24+
# Rest Pattern and Parameters
2525

2626
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.
2727

@@ -94,3 +94,75 @@ The subtle distinction that tells you when and where to use spread and rest.
9494

9595
- Use Spread when you want to write values seperated by commas.
9696
- 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.
128+
129+
```js
130+
restaurant.numGuests = 23;
131+
const guest = restaurant.numGuests ? restaurant.numGuests : 10;
132+
console.log(guest);
133+
134+
// 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+
const guest2 = 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.
145+
146+
For Example
147+
148+
```js
149+
// Short Circuting (AND &&)
150+
console.log(0 && "Eke");
151+
console.log(7 && "Eke");
152+
153+
console.log("Hello" && 23 && null && "Eke");
154+
155+
if (restaurant.orderPizza) {
156+
restaurant.orderPizza("Mushrooms", "Spinach");
157+
}
158+
159+
restaurant.orderPizza && restaurant.orderPizza("Mushrooms", "Spinach");
160+
```
161+
162+
In summary:
163+
164+
- 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.

09-Data-Structure-Modern-Operators-and-Strings/script.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,30 @@ add(...x);
240240

241241
restaurant.orderPizza("Mushrooms", "Onions", "Olives", "Spinach");
242242
restaurant.orderPizza("Mushrooms");
243+
244+
// Short Circuting (OR ||)
245+
console.log("🔸Short Circuting🔸");
246+
console.log(3 || "Eke");
247+
console.log("" || "Eke");
248+
console.log(true || 0);
249+
console.log(undefined || null);
250+
console.log(undefined || 0 || "" || "Hello" || 23 || null);
251+
252+
restaurant.numGuests = 23;
253+
const guest = restaurant.numGuests ? restaurant.numGuests : 10;
254+
console.log(guest);
255+
256+
const guest2 = restaurant.numGuests || 10;
257+
console.log(guest2);
258+
259+
// Short Circuting (AND &&)
260+
console.log(0 && "Eke");
261+
console.log(7 && "Eke");
262+
263+
console.log("Hello" && 23 && null && "Eke");
264+
265+
if (restaurant.orderPizza) {
266+
restaurant.orderPizza("Mushrooms", "Spinach");
267+
}
268+
269+
restaurant.orderPizza && restaurant.orderPizza("Mushrooms", "Spinach");

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
</head>
1111
<body>
1212
<h1>Console.log</h1>
13-
<script src="02-Fundamentals-Part-2/15_looping_arrays_breaking_continuing/script.js"></script>
13+
<script src="09-Data-Structure-Modern-Operators-and-Strings/script.js"></script>
1414
</body>
1515
</html>

0 commit comments

Comments
 (0)