Logical Operators
In short: Operators that combine several truth values (often results of comparisons) with each other — AND (&&), OR (||), and NOT (!).
In more detail: && only returns true if BOTH sides are true; || is enough once ONE side is true. Most languages evaluate this with “short-circuiting” (short-circuit evaluation): for &&, the second condition isn’t checked at all if the first is already false, because the overall result is already determined anyway — this can be deliberately used to avoid expensive or risky checks.
In Depth
The truth tables of the three basic operators:
true && true -> true
true && false -> false
false && false -> false
true || true -> true
true || false -> true
false || false -> false
!true -> false
!false -> trueShort-circuit evaluation is more than a pure performance optimisation — it’s actively used as a language feature to avoid errors:
// Without short-circuiting, the second part would throw an error for null
if (user !== null && user.age >= 18) {
// user.age is ONLY checked if user is guaranteed to not be null
}If user were null, user.age would otherwise trigger a runtime error (accessing a field of a non-existent object) — thanks to short-circuiting, this second part isn’t evaluated at all, because user !== null already gave false and && therefore HAS to return false anyway.
A common beginner mistake with several logical operators in one condition is incorrect grouping — a && b || c isn’t necessarily read the way you’d intuitively expect, since && has higher priority than || in most languages (see Operator Precedence) and is therefore evaluated first. Explicit parentheses ((a && b) || c) make the intent unambiguous and save debugging time.
XOR: the less commonly used fourth basic operator
Besides AND/OR/NOT, there’s also “exclusive OR” (XOR), which only returns true if EXACTLY ONE of the two sides is true — not both, not neither. In most higher-level programming languages there’s no dedicated logical operator for this (unlike AND/OR), instead XOR is usually reproduced via the bitwise operator (e.g. ^) or an inequality check between two boolean values (a != b), since both give the same result for boolean values.
Cross-language notation
The specific syntax for logical operators differs between languages: &&/||/! in most C-like languages (Java, JavaScript, C++), but and/or/not as spelled-out keywords in Python. The underlying concept (short-circuit evaluation, truth tables) stays identical across all languages — only the notation differs.
See also: Comparison Operators, Operators, if…else, Operator Precedence