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: README.md
+8-16Lines changed: 8 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3029,9 +3029,6 @@ for (let i = 0; i < 10; i++) {
3029
3029
}
3030
3030
```
3031
3031
3032
-
**Real-World Use Case:**
3033
-
In a Node.js REST API, `if/else` chains validate incoming request payloads (checking required fields, type constraints, and business rules) before passing control to the service layer — keeping the controller logic clean and the happy path unindented.
3034
-
3035
3032
<div align="right">
3036
3033
<b><a href="#table-of-contents">↥ back to top</a></b>
3037
3034
</div>
@@ -3079,9 +3076,6 @@ if (value) {
3079
3076
}
3080
3077
```
3081
3078
3082
-
**Real-World Use Case:**
3083
-
In a React component rendering a dashboard widget, an `if...else if` ladder checks the API response status (`"loading"`, `"error"`, `"empty"`, `"success"`) and returns the appropriate JSX. Placing the `"loading"` state first ensures the spinner is shown instantly while data fetches, improving perceived performance.
3084
-
3085
3079
<div align="right">
3086
3080
<b><a href="#table-of-contents">↥ back to top</a></b>
3087
3081
</div>
@@ -3155,9 +3149,6 @@ switch (x) {
3155
3149
}
3156
3150
```
3157
3151
3158
-
**Real-World Use Case:**
3159
-
In a Redux reducer, a `switch` on `action.type` is the canonical pattern. Each `case` maps to a specific action string (e.g., `"INCREMENT"`, `"DECREMENT"`, `"RESET"`), returning a new state object. This pattern is readable, scalable, and directly documented in the Redux style guide.
3160
-
3161
3152
<div align="right">
3162
3153
<b><a href="#table-of-contents">↥ back to top</a></b>
3163
3154
</div>
@@ -3225,7 +3216,8 @@ for (const val of arr) {
3225
3216
}
3226
3217
```
3227
3218
3228
-
**Real-World Use Case:**
3219
+
**Real-World Use Case:**
3220
+
3229
3221
In a Node.js data-processing pipeline, `for...of` with `await` (inside an `async` function) iterates over a paginated API result set sequentially — something `forEach` cannot do since it ignores returned Promises. `for...in` is used to dynamically enumerate config object keys when serializing environment-specific settings.
3230
3222
3231
3223
<div align="right">
@@ -3278,9 +3270,6 @@ while (true) {
3278
3270
console.log(counter); // 5
3279
3271
```
3280
3272
3281
-
**Real-World Use Case:**
3282
-
A `do...while` loop is ideal for a CLI tool (e.g., a Node.js interactive script using `readline`) where you always need to display a menu at least once and re-display it until the user selects "Exit". A `while` loop suits polling a queue (`while (queue.length > 0)`) in a job processor, since the queue might be empty on startup.
3283
-
3284
3273
<div align="right">
3285
3274
<b><a href="#table-of-contents">↥ back to top</a></b>
3286
3275
</div>
@@ -3336,6 +3325,7 @@ outerLoop: for (let i = 0; i < 3; i++) {
3336
3325
```
3337
3326
3338
3327
**Real-World Use Case:**
3328
+
3339
3329
In a search autocomplete engine, `break` short-circuits a loop over a large dataset once the desired number of suggestions is collected, preventing unnecessary iterations. `continue` is used in data-cleaning pipelines to skip `null` or malformed records without nesting the entire logic in an extra `if` block — keeping cyclomatic complexity low and the code easier to review.
3340
3330
3341
3331
<div align="right">
@@ -3392,6 +3382,7 @@ if (isLoggedIn) {
3392
3382
```
3393
3383
3394
3384
**Real-World Use Case:**
3385
+
3395
3386
In React, the ternary operator is the standard pattern for conditional rendering within JSX since JSX is an expression context — `if` statements cannot appear inside `{}` interpolation. For example, rendering a loading spinner vs. a data table based on `isLoading` state is universally handled with a ternary.
When consuming third-party REST APIs or GraphQL responses, deeply nested fields may be absent depending on the query. Optional chaining eliminates dozens of guard clauses like `if (response && response.data && response.data.user)`. Nullish coalescing is critical when a user-configurable setting has `0` or `""` as a valid value — falling back with `||` would incorrectly override those legitimate settings.
3447
3439
3448
3440
<div align="right">
@@ -3511,6 +3503,7 @@ searchGrid: for (let i = 0; i < rows; i++) {
3511
3503
```
3512
3504
3513
3505
**Real-World Use Case:**
3506
+
3514
3507
In a pathfinding algorithm (e.g., BFS/DFS on a 2D game map) or a seat-selection engine scanning rows and seats in a stadium booking system, a labeled `break` exits all nested loops the moment a valid seat is located — avoiding a boolean flag variable that would otherwise pollute the outer scope and add an extra condition to every loop header.
3515
3508
3516
3509
<div align="right">
@@ -3570,7 +3563,8 @@ if (isAuthenticated) {
3570
3563
}
3571
3564
```
3572
3565
3573
-
**Real-World Use Case:**
3566
+
**Real-World Use Case:**
3567
+
3574
3568
In React, `&&` short-circuit is the idiomatic pattern for **conditional rendering**: `{isLoggedIn && <UserMenu />}` renders the component only when `isLoggedIn` is truthy, with no JSX `if` block needed. The `||` operator is universally used for **default parameter fallbacks** before optional chaining became standard. In Node.js middleware chains, `&&` guards ensure a dependency (e.g., a database connection) is available before proceeding to query logic.
3575
3569
3576
3570
<div align="right">
@@ -17286,8 +17280,6 @@ onTTFB(sendToAnalytics); // Time to First Byte — target < 800ms
17286
17280
| Migrate to streaming SSR | High | Medium | Later |
17287
17281
| WebAssembly for a single CPU-bound algorithm | Very High | Low | Probably never |
17288
17282
17289
-
**Real-World Use Case:**
17290
-
17291
17283
<div align="right">
17292
17284
<b><a href="#table-of-contents">↥ back to top</a></b>
0 commit comments