Operators
Every operator supported by the expr evaluator, organised by arity. Click any entry in the sidebar to jump directly to its reference — you stay on this page throughout.
Unary Operators
Operate on a single operand placed either before or after it. They transform or interrogate one value without needing a second.
Negation
Reverses the sign of a numeric value. Applied as a prefix, it multiplies the operand by −1. Works on integers, floats and the result of any sub-expression enclosed in parentheses.
| x | -x |
|---|---|
| 5 | -5 |
| -3.14 | 3.14 |
| 0 | -0 |
| -(-(47)) | 47 |
-(5 + 3) → -8 -(-12) → 12 -(2 ** 4) → -16
Unary Plus
Attempts to convert the operand to a number. If the operand is already a number it returns it unchanged — no sign flip occurs. Unlike negation, its sole purpose is numeric coercion.
| x | +x |
|---|---|
| 5 | 5 |
| +(-(11)) | -11 |
+5 → 5 +(-(11)) → -11
Logical NOT
Inverts a boolean value. Returns false if the operand is truthy, true if it is falsy. Logical not unary operator supports use with parentheses.
| x | !x |
|---|---|
| true | false |
| false | true |
!true → false !false → true !!false → false !!true → true !(!true) → true !((!true)) → true
Arithmetic Operators
Work on two numeric operands to produce a computed result. Precedence order (high → low): **, then * / %, then + -. Use parentheses to override.
Addition
Adds two numeric operands together. If either operand is a string, concatenation is performed instead. For pure math expressions, both operands should evaluate to numbers.
| A | B | A + B |
|---|---|---|
| 3 | 5 | 8 |
| 10 | -4 | 6 |
| 2.5 | 1.5 | 4 |
(100 + 200) * 3 → 900 1.1 + 2.2 → 3.3 0 + 0 → 0
Subtraction
Subtracts the right operand from the left, returning their arithmetic difference. Unlike the unary -, this always requires two operands placed on either side of the operator.
| A | B | A - B |
|---|---|---|
| 10 | 4 | 6 |
| 3 | 7 | -4 |
| 8.5 | 2.5 | 6 |
1000 - 375 - 125 → 500 (10 - 3) * 2 → 14
Multiplication
Returns the product of two operands. Evaluated before addition and subtraction following standard operator precedence. Use parentheses to override evaluation order.
| A | B | A * B |
|---|---|---|
| 3 | 4 | 12 |
| -2 | 5 | -10 |
| 1.5 | 2 | 3 |
(2 + 3) * (4 - 1) → 15 4 * 0.25 → 1 -3 * -3 → 9
Division
Divides the left operand by the right, returning a floating-point result — it never rounds automatically. Dividing by zero yields no value; dividing zero by zero yields in indeterminate value.
| A | B | A / B |
|---|---|---|
| 10 | 2 | 5 |
| 7 | 2 | 3.5 |
| 1 | 0 | Infinity |
360 / (2 * 6) → 30 1 / 3 → 0.3333333333333333 5 / 0 → No value
Modulo / Remainder
Returns the remainder after dividing the left operand by the right. The sign of the result always matches the sign of the left (dividend) operand. Useful for divisibility checks and cyclic indexing.
| A | B | A % B |
|---|---|---|
| 10 | 3 | 1 |
| 7 | 7 | 0 |
| -9 | 4 | -1 |
17 % 5 → 2 // remainder of 17 ÷ 5 100 % 2 → 0 // even-number check 13 % 4 → 1
Exponentiation
Raises the left operand (base) to the power of the right operand (exponent). Right-associative: 2 ** 3 ** 2 evaluates as 2 ** 9 = 512, not 8 ** 2. Fractional exponents compute roots.
| base | exp | base ** exp |
|---|---|---|
| 2 | 10 | 1024 |
| 3 | 3 | 27 |
| 9 | 0.5 | 3 |
2 ** 8 → 256 9 ** 0.5 → 3 // √9 2 ** 3 ** 2 → 512 // right-associative: 2 ** 9
Comparison Operators
Compare two values and return a boolean — true or false. Results can be used directly or chained with logical operators for compound conditions.
Strict Equality
Returns true if both operands have the same value and the same type — no type coercion is performed. Prefer === over == to avoid subtle bugs from implicit conversions.
| A | B | A === B |
|---|---|---|
| 5 | 5 | true |
| 5 | "5" | false |
| true | 1 | false |
| null | null | true |
5 === 5 → true 5 === "5" → false // different types — no coercion 1 === true → false
Strict Inequality
The inverse of ===. Returns true if the operands differ in value or in type. No type coercion is performed. Prefer !== over != for predictable comparisons.
| A | B | A !== B |
|---|---|---|
| 5 | 3 | true |
| 5 | "5" | true |
| 7 | 7 | false |
10 !== "10" → true // type mismatch 10 !== 10 → false 10 !== 11 → true
Less Than
Returns true if the left operand is strictly less than the right.
| A | B | A < B |
|---|---|---|
| 3 | 5 | true |
| 8 | 2 | false |
| 4 | 4 | false |
3 < 5 → true 5 < 5 → false // equal, not less -1 < 0 → true
Greater Than
Returns true if the left operand is strictly greater than the right.
| A | B | A > B |
|---|---|---|
| 5 | 3 | true |
| 2 | 8 | false |
| 4 | 4 | false |
5 > 3 → true 100 > 100 → false // equal, not greater -1 > -10 → true
Less Than or Equal
Returns true if the left operand is less than or equal to the right.
| A | B | A <= B |
|---|---|---|
| 3 | 5 | true |
| 5 | 5 | true |
| 8 | 2 | false |
5 <= 10 → true 10 <= 10 → true // equal counts 11 <= 10 → false
Greater Than or Equal
Returns true if the left operand is greater than or equal to the right.
| A | B | A >= B |
|---|---|---|
| 10 | 5 | true |
| 5 | 5 | true |
| 3 | 8 | false |
score >= 90 → true // if score is 95 18 >= 18 → true // equal counts 17 >= 18 → false
Logical Operators
Combine or modify boolean expressions. All three short-circuit: evaluation stops as soon as the outcome is determined, without evaluating the remaining operand.
Logical AND
Returns true only when both operands are truthy. Short-circuits: if the left operand is falsy, the right is never evaluated. In non-boolean contexts, returns the first falsy value found, or the last value if all are truthy.
| A | B | A && B |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
true && true → true true && false → false 5 > 3 && 2 < 4 → true // both hold
Logical OR
Returns true if at least one operand is truthy. Short-circuits: if the left is truthy, the right is not evaluated. Returns the first truthy value found, or the last value if all are falsy.
| A | B | A || B |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
true || false → true false || false → false 0 || 42 → 42 // first truthy
Nullish Coalescing
Returns the right operand only when the left is null or undefined. Unlike ||, it does not treat 0, "", or false as nullish — making it the safer choice for default values when those are legitimate inputs.
| Left | Right | Left ?? Right |
|---|---|---|
| null | "default" | "default" |
| undefined | 42 | 42 |
| 0 | "fallback" | 0 |
| "" | "fallback" | "" |
| false | true | false |
null ?? "default" → "default" undefined ?? 0 → 0 0 ?? "fallback" → 0 // 0 is not null/undefined "" ?? "empty" → "" // "" is not null/undefined
Ternary Operator
JavaScript's only operator that takes three operands. Provides a concise inline alternative to an if / else statement.
Conditional
Syntax: condition ? valueIfTrue : valueIfFalse. Evaluates the condition; if truthy returns the first branch, otherwise returns the second. Can be nested for multi-branch logic, though deep nesting hurts readability — prefer a lookup object instead.
| condition | Result returned |
|---|---|
| true | valueIfTrue |
| false | valueIfFalse |
score >= 90 ? "A" : "B" // "A" if score ≥ 90 x > 0 ? "positive" : "non-positive" age >= 18 ? "green" : "blue" n % 2 === 0 ? "even" : "odd"