Arithmetic
The four basic operations work on numeric values:Unary Operators
Prefix- negates a value, prefix + coerces to number:
String Concatenation
The+ operator does double duty: if either side is a string, it concatenates. Otherwise it adds numerically.
Comparison
Six comparison operators, all returning booleans:| Operator | Meaning |
|---|---|
== | Equal (loose) |
!= | Not equal |
> | Greater than |
>= | Greater than or equal |
< | Less than |
<= | Less than or equal |
Logical
Boolean operators for combining conditions:| Operator | Meaning |
|---|---|
&& | Logical AND |
|| | Logical OR |
! | Logical NOT |
&& returns the first falsy value (or the last value if all truthy), || returns the first truthy value (or the last value if all falsy). This makes || useful as a default mechanism:
name is undefined, empty, or any other falsy value, the result is 'Anonymous'.
Ternary
The ternary operatorcondition ? ifTrue : ifFalse selects between two values. It’s useful for producing conditional text inline:
Ternaries nest for multi-way selection:
Precedence
From lowest (loosest binding) to highest (tightest binding):| Precedence | Operators |
|---|---|
| Pipe | | |
| Ternary | ? : |
| Logical OR | || |
| Logical AND | && |
| Logical NOT | ! |
| Comparison | == != > < >= <= |
| Addition | + - |
| Multiplication | * / |
| Unary | - + |
price * quantity | currency evaluates as (price * quantity) | currency — the arithmetic happens first, then the pipe formats the result. Use parentheses to override: (a + b) * c.