Skip to main content

Precedence

Operators are listed from lowest to highest precedence. All binary operators are left-associative. Use parentheses to change the order of evaluation. There’s no modulo operator (%), no compound assignment (+=) and no increment or decrement operator (++, --).

Logical operators

! (not), && (and) and || (or) require booleans. Using them with any other type is an error. There’s no “truthiness”: 0, "" and null are not treated as false. && and || short-circuit: the right side is evaluated only when it’s needed.

Equality

== and != work on any two values.
  • Numbers are compared by value across types, so 1 == 1.0 is true.
  • Objects and lists are compared deeply, by contents rather than identity: [1, 2] == [1, 2] is true.
  • Values of different non-numeric types are never equal. For example, null == false is false and "1" == 1 is false.

Comparison

<, <=, > and >= compare two numbers by value, or two strings lexicographically. Comparing any other combination, such as a number with a string, is an error.

Arithmetic

+, -, * and / work on numbers. The result type depends on the operand types:

Integer arithmetic

  • A result outside the range of its type is an overflow error. Integers never wrap around.
  • Integer division truncates toward zero: 7 / 2 is 3 and -7 / 2 is -3.
To get a fractional result from two integers, make one operand a decimal: 7 / 2.0 is 3.5.

Decimal arithmetic

  • Addition, subtraction and multiplication are exact.
  • Division rounds half away from zero to 10 decimal places, or to the number of decimal places of the more precise operand if that’s greater. For example, 1 / 3.0 is 0.3333333333.

Division by zero

Dividing by zero is an error for both integers and decimals.

String concatenation

When either side of + is a string, the other side is converted to a string and the two are joined. Values are converted the same way as in string interpolation.
This is the only implicit conversion to a string. -, * and / never accept strings.

Unary minus

Unary - negates a number: -5, -price, -(a + b).