Boolean logic in JavaScript: &&, ||, !, truthiness and precedence
JavaScript writes the operators as !, && and ||, ranks them in that order, and lets any value stand in for a Boolean through truthiness. There is no JavaScript export in Tangibool; this page is a translation guide.
| Logic | JavaScript | Note |
|---|---|---|
NOT A | !a | Coerces; !!a normalises to a Boolean |
A AND B | a && b | Returns an operand; short-circuits |
A OR B | a || b | Returns an operand; ?? is different |
A XOR B | a !== b on Booleans | ^ gives a number |
A -> B | !a || b | No implication operator |
A <-> B | a === b on Booleans | Strict equality |
TRUE, FALSE | true, false | Lower case; eight falsy values exist |
!a, a && b and a || b are the logical operators, with ! binding tightest, then &&, then ||, the same order as NOT, AND, OR here. Like Python, && and || short-circuit and return an operand: 0 || 'default' is 'default', and user && user.name is user when user is null. The nullish operator ?? is not a logical OR: it falls through only on null and undefined, not on 0, '' or false.
Truthiness is the trap. The falsy values are false, 0, -0, 0n, '', null, undefined and NaN; everything else, including 'false', [] and {}, is truthy. A rule evaluated on such values is not the rule evaluated on TRUE and FALSE, so normalise with Boolean(x) or !!x at the boundary and compare with ===. The engine's semantics have two values and no coercion, which is the point of writing the rule there first.
The single-character operators &, |, ^ and ~ convert their operands to 32-bit integers, so true & true is 1, a number, and ~true is -2. For exclusive or on Booleans use a !== b; for implication write !a || b. The parser here rejects &, |, ^ and ~ with a repair, because a formula copied from JavaScript with those operators would otherwise change meaning silently.
The common slip
Treating if (value) as a test for true. It is a test for truthiness: an empty string, zero and NaN take the false branch, and the string 'false' takes the true one. Convert at the boundary, then reason about two values.
Questions
Can Tangibool export JavaScript?
No. The exports are Python, SQL, SMT-LIB and LaTeX, plus CSV and Markdown for tables. For JavaScript, translate with the table above and keep the truth table as the test oracle.
Is a || b the inclusive OR?
On two Booleans, yes, and it is TRUE when both are. On other values it returns the first truthy operand, which is why 0 || 5 is 5 rather than true.
Related
See also
- Boolean logic in Python: and, or, not, precedence and common mistakes
- Boolean logic in Java: &&, ||, ! vs &, | and ^
- Boolean logic in C: logical vs bitwise operators
- Single & and | are bitwise in most languages
- ^ means XOR in C, Java and Python, not AND or power
- ~ is bitwise NOT in code, logical NOT in some textbooks
- Double equals (==) compares values; for logical equivalence use ↔
- Deriving test cases from a truth table without testing every row
Languages
Generated from the Tangibool engine, semantics tangibool-semantics/1, engine 1.0.0, build . Every table, map, simplification and verdict on these pages was computed by the Tangibool engine when the site was built, and every claim was re-checked by the same exact checker the app uses. Limits are stated where they apply. © 2026 Foreranger LTD.