Suggest an editImprove this articleRefine the answer for “The && operator”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**The `&&` operator** in JavaScript is **logical AND**. It is used not only for logical comparisons but also for **controlling the execution of expressions (short-circuit evaluation)**. **Key point:** if `a` is falsy, `a` is returned; otherwise `b` is returned.Shown above the full answer for quick recall.Answer (EN)ImageThe `&&` operator in JavaScript is **logical AND**. It is used not only for logical comparisons but also for **controlling the execution of expressions (short-circuit evaluation)**. --- ## Basic meaning ```javascript a && b ``` Returns: - the value of `a`, if `a` is *falsy*; - otherwise - the **value** of `b`. --- ## How it works (step by step) 1. JS evaluates the first expression `a`. 2. If `a` is **falsy** → it **immediately returns** `a` (the rest is not evaluated). 3. If `a` is **truthy** → it returns the **second expression** `b`. --- ## Examples with boolean values ```javascript true && true // true true && false // false false && true // false false && false // false ``` Same as in logic: `A ∧ B` = "true if both are true". --- ## Examples with other types > JS does not coerce values to `true`/`false`, > it **returns one of the expressions themselves.** ```javascript console.log(1 && 2); // 2 (both truthy, returns the second) console.log(0 && 2); // 0 (first is falsy) console.log('hi' && 123); // 123 console.log(null && 'ok'); // null console.log(true && 'JS'); // "JS" ``` --- ## Short-circuit If the first value is falsy, the second is not even **evaluated**. ```javascript false && console.log('Will not run'); 0 && console.log('Will not run'); null && console.log('Will not run'); ``` > This lets you use `&&` for **conditional execution**: ```javascript isLoggedIn && showDashboard(); ``` The call to `showDashboard()` happens **only if** `isLoggedIn === true`. --- ## Example in real code ```javascript const user = { name: 'Tim' }; console.log(user && user.name); // "Tim" const guest = null; console.log(guest && guest.name); // null (no error!) ``` > This is handy for safe property access (before `?.` - optional chaining - existed). --- ## Difference from `if` ```javascript if (user) { console.log(user.name); } // same thing user && console.log(user.name); ``` > `&&` just does the same thing more concisely. --- ## Example in JSX (React) ```javascript {isAdmin && <AdminPanel />} ``` > The `<AdminPanel />` component renders **only if** `isAdmin === true`. --- ## Truthy / falsy table | falsy values | truthy values | |---|---| | `false` | everything else | | `0`, `-0`, `0n` | `'strings'` | | `''` (empty string) | `[]`, `{}` | | `null` | `true` | | `undefined` | any objects | | `NaN` | functions | --- ## Summary | Expression | Result | Explanation | |---|---|---| | `true && "JS"` | `"JS"` | both truthy → returns the second | | `false && "JS"` | `false` | first is falsy | | `null && 5` | `null` | first is falsy | | `"hi" && 0` | `0` | second is falsy | | `1 && 2 && 3` | `3` | all truthy → returns the last | | `0 && 2 && 3` | `0` | first is falsy → stops | --- ## In short - `a && b` → if `a` is falsy, returns `a`; otherwise returns `b`. - Used for: - one-line condition checks, - short function calls, - safe property access, - JSX rendering.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.