The AND operator in JavaScript is the double ampersand &&, and it returns true only when both operands are truthy. It also has a second behavior: it returns the first falsy operand, or the last operand if all are truthy, which makes it useful for conditional execution and default values.
How the Logical AND Operator Works
In its simplest form, && evaluates two boolean values. For example, true && true returns true, while true && false returns false. The operator uses short-circuit evaluation: if the left operand is falsy, JavaScript never evaluates the right operand and returns the left operand directly. If the left operand is truthy, it evaluates and returns the right operand.
This means && does not always return a boolean. It returns one of the original operand values. For instance:
0 && "hello"returns0(because 0 is falsy)"hello" && "world"returns"world"(both truthy, so last one)null && 42returnsnull
This behavior is consistent with ECMAScript specification and is a common source of confusion for beginners who expect a boolean result.
Truthy and Falsy Values in JavaScript
To understand && fully, you need to know which values are considered falsy in JavaScript. There are exactly six falsy values:
false0""(empty string)nullundefinedNaN
Everything else—including "0", [], {}, and "false"—is truthy. This list is fixed; no other values are falsy. When you use &&, JavaScript converts each operand to a boolean for the decision, but returns the original value, not the converted boolean.
Short-Circuit Evaluation and Its Practical Uses
Short-circuiting means the right operand is only evaluated if the left operand is truthy. This is useful for avoiding errors. A classic example is checking if an object exists before accessing a property:
const user = null;
const name = user && user.name; // returns null, no error
If user were null, evaluating user.name would throw a TypeError. With &&, the right side is skipped, so no error occurs. Another common use is conditional rendering in React: {isLoggedIn && <Dashboard />}. If isLoggedIn is false, the component is not rendered.
You can also chain multiple && operators. Evaluation happens left to right, and the first falsy value encountered is returned. If none are falsy, the last value is returned. For example, a && b && c returns a if a is falsy, otherwise b if b is falsy, otherwise c.
Difference Between && and &
JavaScript has a single ampersand & which is the bitwise AND operator. It operates on the binary representations of numbers. For example, 5 & 3 returns 1 because 5 in binary is 101 and 3 is 011, and the AND of those bits is 001 which is 1. The logical && works with any values and returns operands, not numbers. They are completely different and should not be confused. Using & on non-numbers can produce unexpected results because JavaScript converts operands to 32-bit integers.
Common Mistakes and Misconceptions
One frequent mistake is assuming && always returns a boolean. As shown, it returns an operand value. Another misconception is that 0 && "x" returns false—it actually returns 0. This matters when you store the result in a variable or pass it to a function.
Another pitfall is using && for default values when the left side could be a valid falsy value. For example, const count = userInput && userInput.length will return 0 if userInput is an empty string, which might be unintended. In such cases, consider the nullish coalescing operator ?? which only falls back on null or undefined.
Also, be careful with operator precedence. && has higher precedence than ||, so a || b && c is parsed as a || (b && c). If you need a different grouping, use parentheses.
Using && with Other Logical Operators
The logical OR operator || returns the first truthy operand or the last falsy one. Combined with &&, they can express complex conditions. For example, (a && b) || c returns c if either a or b is falsy, otherwise returns b (the last truthy). The nullish coalescing operator ?? is different: it returns the right operand only when the left is null or undefined, not for other falsy values like 0 or "". This distinction is crucial when writing robust code.
In modern JavaScript, you can also use &&= (logical AND assignment). This operator assigns the right value only if the left variable is truthy. For example:
let x = 1;
x &&= 5; // x becomes 5
let y = 0;
y &&= 5; // y stays 0
This is a shorthand for x = x && 5 and is supported in all modern browsers and Node.js since 2020.
Performance and Readability Considerations
Short-circuiting can improve performance by avoiding unnecessary function calls or heavy computations. For example, isValid && expensiveFunction() only calls expensiveFunction when isValid is true. However, overusing && for side effects can hurt readability. Many style guides recommend using if statements for clarity when the right operand has side effects, and reserving && for simple value selection or guard clauses.
When writing code, prefer explicit boolean returns if you truly need a boolean. For instance, Boolean(a && b) or !!(a && b) converts the result to a boolean. This is often clearer for conditions in if statements, though not strictly necessary because if already coerces.
Frequently Asked Questions
Does && return a boolean?
No, not always. It returns the first falsy operand or the last operand if all are truthy. Only when both operands are booleans does it return a boolean. For example, true && false returns false, but 1 && 2 returns 2.
What is the difference between && and ||?
&& returns the first falsy value or the last truthy value. || returns the first truthy value or the last falsy value. They are opposites. For example, 0 || "default" returns "default", while 0 && "default" returns 0.
Can I use && to set default values?
Yes, but only when the default should apply to all falsy values. For example, const name = input && input.name will use input.name only if input is truthy. If you want to keep 0 or "" as valid, use ?? instead.
Why does && not work in my if statement?
It likely does work, but you might be expecting a boolean. For example, if (0 && "x") is false because 0 is falsy. However, if (" " && "x") is true because a space is truthy. Check your operand values—empty strings, null, and 0 are falsy.
Is && supported in all browsers?
Yes, && has been part of JavaScript since its inception and is supported everywhere. The assignment variant &&= is newer but supported in all modern browsers (Chrome 85+, Firefox 79+, Safari 14+, Edge 85+).
How do I force a boolean result from &&?
Use Boolean(a && b) or double negation !!(a && b). Both convert the result to a strict boolean. This is useful when storing the result in a variable that you expect to be true or false.
Related
- Excel Conditional Formatting: How It Works
- Power Query Group By: Keep All Columns?
- Integer Range Partitioning in BigQuery
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Wedding Budget Spreadsheet: Free Online Options and SetupYes. You can make a wedding budget spreadsheet online for free in Google Sheets or Excel for the…
- Excel Column Letters vs Numbers: How References WorkExcel uses letters to label columns in its standard A1 reference style, while numbers describe each column’s position:…
- Remove Quotation Marks in Excel: Methods That WorkTo remove straight double quotation marks from text in Excel, use Find and Replace with " as the…
- Excel: Freeze the Top Row and First ColumnYes. In Excel for Microsoft 365 on Windows, select cell B2, then choose View > Freeze Panes >…
- How to Insert an Image in Microsoft Excel: Step-by-StepTo insert an image in Microsoft Excel for Microsoft 365 on Windows, select the worksheet, open Insert >…
- Excel Inventory Tracker Template: How to Build OneThe fastest way to get an Excel inventory tracker is to use a built-in template: open Excel, go…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.