Suggest an editImprove this articleRefine the answer for “Conditional constructs”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**Conditional constructs** in JavaScript include `if/else if/else` (the main and most readable way to branch code), the ternary operator `? :`, `switch`, and the logical operators `&&`, `||`, `??`. **Key point:** `??` (nullish coalescing) differs from `||` in that it does not trigger on `0`, `''`, or `false`, only on `null` and `undefined`.Shown above the full answer for quick recall.Answer (EN)Image## 1. `if`, `else if`, `else` This is the **main** and most readable construct for branching code. ```javascript const age = 20; if (age >= 18) { console.log('Adult'); } else if (age >= 14) { console.log('Teenager'); } else { console.log('Child'); } ``` - `if` checks a condition - `else if` adds additional checks - `else` runs if all the previous ones did not match > The condition inside `if` is converted to a **boolean value** (truthy/falsy). --- ## 2. The ternary operator `? :` A short form for simple conditions. ```javascript const age = 18; const status = age >= 18 ? 'Adult' : 'Minor'; console.log(status); // "Adult" ``` Syntax: `condition ? value_if_true : value_if_false` You can nest several ternary operators (but be careful with readability): ```javascript const age = 25; const group = age < 13 ? 'child' : age < 20 ? 'teenager' : 'adult'; ``` --- ## 3. The `switch` statement Convenient when there are many variants of the same expression. ```javascript const day = 3; switch (day) { case 1: console.log('Monday'); break; case 2: console.log('Tuesday'); break; case 3: console.log('Wednesday'); break; default: console.log('Unknown day'); } ``` - `break` prevents "falling through" into the next `case` - `default` is the equivalent of `else` You can use **grouping**: ```javascript switch (day) { case 6: case 7: console.log('Day off'); break; default: console.log('Workday'); } ``` --- ## 4. The logical operators `&&`, `||`, `??` Sometimes `if` can be **replaced** with logical shorthand. ### `&&`: "and" (runs the second operand if the first is true) ```javascript const isAdmin = true; isAdmin && console.log('Welcome, admin!'); ``` > If `isAdmin === true`, `console.log` will run. --- ### `||`: "or" (returns the first truthy value) ```javascript const userName = '' || 'Guest'; console.log(userName); // "Guest" ``` > Convenient for default values. --- ### `??`: "nullish coalescing" (only for `null` and `undefined`) ```javascript const userInput = null; const value = userInput ?? 'Default'; console.log(value); // "Default" ``` > Difference from `||`: it does not trigger on `0`, `''`, `false`.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.