Conditional Statements
JavaScript uses
if, else if, and else to control the execution path.javascript
Comparisons: The switch Statement
The
switch is a cleaner alternative for multiple discrete values.javascript
Loops: for and while
1. Traditional for Loop
Classic C-style Loop with a counter.
javascript
2. while Loop
Better for when you don't know the exact end point.
javascript
Modern Loops: for...of and for...in
Avoid using traditional loops for arrays or objects. ES6 introduced cleaner syntax.
for...of (Iterating through ARRAYS)
Used for any iterable (array, string, set).
javascript
for...in (Iterating through OBJECT KEYS)
Used specifically for looping over object properties.
javascript
Loop Control: break and continue
break: Immediately exits the entire loop.continue: Skips the current iteration and jumps to the next one.
javascript
Summary
| Feature | Syntax | Best Use... |
|---|---|---|
| if/else | if (x) {} | General logic |
| switch | switch (x) {} | Multiple discrete values |
| for | for (i; i < n; i++) | Counter-driven loops |
| while | while (condition) | Condition-driven loops |
| for...of | for (x of arr) | Iterating array values |
| for...in | for (key in obj) | Iterating object keys |
| break | break | Terminate loop |
| continue | continue | Skip iteration |
| Best For | Logic, Iteration, Control | |
| Indentation | Two spaces (Recommended) | |
| Ternary | x ? a : b | Concise if/else |
| Optional Chaining | obj?.prop | Safe access (Prevents errors!) |
| Short-Circuit | isAdmin && show() | Simple logical triggers |