Skip to main content

Operators

Arithmetic

+ // addition / string concat / array append or concat / object merge
- // subtraction
* // multiplication
/ // division (error on division by zero)
% // modulo (integer types only; error on modulo by zero)

Integer division by INT_MIN / -1 and LLONG_MIN / -1 saturates to the type's maximum instead of trapping. The same guard applies to % (it would otherwise return 0 rather than trap).

A name immediately after a number is an error — write the operator explicitly:

var y = 5
var z = 99 * y // correct
// var z = 99y // error: unexpected 'y' after number '99'

Arrays with +

[1, 2] + [3, 4] -> [1, 2, 3, 4] // concatenate
[1, 2] + 3 -> [1, 2, 3] // append
3 + [1, 2] -> [3, 1, 2] // prepend
arr += value // append in place (same-type)

Objects with +

{a: 1} + {b: 2} -> {a: 1, b: 2} // merge; right side wins on conflicts
obj += {key: val} // merge in place

Unary and compound

- // numeric negation
! // logical NOT

= += -= *= /= %= // compound assignment (add_assign avoids O(n²))
x++ // postfix increment
x-- // postfix decrement

Comparison

== != > < >= <=
  • Cross-numeric comparisons (e.g. int vs float) promote to double without losing integer precision above 2^53.
  • String comparisons are lexicographic.
  • char participates as a number ('a' + 1 is 98).
  • Comparing incompatible types (e.g. int vs string) is an error.
  • nil == nil is true.
  • Functions never compare equal — even f == f is false.

Logical

&& and || short-circuit:

if (a && b) { ... }
if (a || b) { ... }

Ternary

The condition must be parenthesized:

var label = (score >= 60) ? "pass" : "fail"

Precedence

Highest to lowest:

PrecedenceOperators
1 (highest)Unary ! -
2Multiplicative * / %
3Additive + -
4Relational < > <= >=
5Equality == !=
6Logical AND &&
7Logical OR &#124;&#124;
8Ternary ? :
9Assignment = += -= *= /= %= ++ --
10 (lowest)Describe ->

Numeric promotion

LeftOpRightResultNotes
intopintintsaturates on overflow
intopfloatfloat
floatopfloatfloat
longoplonglongsaturates on overflow
intoplonglong
longopfloatdoubleno precision loss through float
charopnumberdouble
anythingopdoubledouble

For + - * / on integral long ∘ float, the long is promoted through double (previously through float, which lost everything past 24 bits: 16777217L + 1.0f collapsed to 16777216).

String concatenation: when either operand of + is a string, both are converted to strings and concatenated.