Operators

Built-in operations that can be applied to primitive values.

Unary Operators

These are operations involving only a single expressions.

Negate (-)

Used to negate a numeric value; negative values become positive and positive values become negative.

const POSITIVE = 123;
const NEGATIVE = -45.6;

const NOW_NEGATIVE = -POSITIVE;                   // -123
const NOW_POSITIVE = -NEGATIVE;                   // 45.6

const DOUBLE_NEGATED = --123;                     // 123

const NEGATED_EXPRESSION = -(123 + 456 / 789);    // -123.57794677

Absolute (+)

Used to get the absolute version of a numeric value; negative values become positive.

const NEGATIVE = -45.6;
const POSITIVE = 123;

const NOW_POSITIVE = +NEGATIVE;                    // 45.6
const STILL_POSITIVE = +POSITIVE;                  // 123

const ABSOLUTE_NEGATIVE = +-123;                   // 123

const ABSOLUTE_EXPRESSION = +(123 - 456 * 789);    // 359661

Not (!)

Used to invert a boolean value; true values become false and false values become true.

const TRUE = true;
const FALSE = false;

const NOW_FALSE = !TRUE;                    // false
const NOW_TRUE = !FALSE;                    // true

const CANCEL_NOT = !!true;                  // true

const NOT_EXPRESSION = !(true || false);    // true

Binary Operators

These are operations involving both a left-hand side and a right-hand side expression.

Logical Operators

These operations are used to compare boolean values.

Logical AND (&&)

This operation evaluates to true if both the left-hand and right-hand expressions evaluate to true and false otherwise.

const TRUE = true && true;                                    // true
const FALSE = true && false;                                  // false

const AND_EXPRESSION = (123 == (100 + 23)) && (700 > 903);    // false

Logical OR (||)

This operation evaluates to true if either the left-hand or right-hand expressions evaluate to true and false otherwise.

const TRUE = true || false;                                  // true
const FALSE = false || false;                                // false

const OR_EXPRESSION = (123 == (100 + 23)) || (700 > 903);    // true

Comparative Operators

These operations are used to compare numeric (integer and float) values.

These operators can compare values with asymmetric types, meaning that an integer can be compared directly to a float.

Less Than or Equal (<=)

This operation evaluates to true if the left-hand value is less than or equal to the right-hand value and false otherwise.

const LESS_THAN = 1 <= 2.1;      // true
const EQUAL_TO = 5.0 <= 5;       // true
const GREATER_THAN = 10 <= 2;    // false

Less Than (<)

This operation evaluates to true if the left-hand value is less than the right-hand value and false otherwise.

const LESS_THAN = 1 < 2.1;      // true
const EQUAL_TO = 5 < 5;         // false
const GREATER_THAN = 10 < 2;    // false

Greater Than or Equal (>=)

This operation evaluates to true if the left-hand value is greater than or equal to the right-hand value and false otherwise.

const LESS_THAN = 1 >= 2.1;      // false
const EQUAL_TO = 5.0 >= 5;       // true
const GREATER_THAN = 10 >= 2;    // true

Greater Than (>)

This operation evaluates to true if the left-hand value is greater than the right-hand value and false otherwise.

const LESS_THAN = 1 > 2.1;      // false
const EQUAL_TO = 5.0 > 5;       // false
const GREATER_THAN = 10 > 2;    // true

Arithmetic Operators

These operations are used to perform basic arithmetic (add, subtract, divide, etc.) on numeric types (integer and float).

These operators can compare values with asymmetric types, meaning that an integer can be compared directly to a float.

Add (+)

This operation adds two numeric values together. If either value is a float then the resulting type is also float otherwise it is integer.

const TWO_INTEGERS = 123 + 456;          // integer
const TWO_FLOATS = 12.3 + 4.56;          // float
const INTEGER_AND_FLOAT = 123 + 4.56;    // float

Subtract (-)

This operation subtracts one numeric value from another. If either value is a float then the resulting type is also float otherwise it is integer.

const TWO_INTEGERS = 123 - 456;          // integer
const TWO_FLOATS = 12.3 - 4.56;          // float
const INTEGER_AND_FLOAT = 123 - 4.56;    // float

Multiply (*)

This operation multiplies two numeric values together. If either value is a float then the resulting type is also float otherwise it is integer.

const TWO_INTEGERS = 123 * 456;          // integer
const TWO_FLOATS = 12.3 * 4.56;          // float
const INTEGER_AND_FLOAT = 123 * 4.56;    // float

Divide (/)

This operation divides one numeric value by another. No matter the type of the left-hand or right-hand values the result is always of type float.

const TWO_INTEGERS = 123 / 456;          // float
const TWO_FLOATS = 4.56 / 4.56;          // float
const INTEGER_AND_FLOAT = 123 / 4.56;    // float

Exponentiation (^)

This operation calculates one numeric value to the power of another. No matter the type of the left-hand or right-hand values the result is always of type float.

const TWO_INTEGERS = 123 ^ 456;          // float
const TWO_FLOATS = 4.56 ^ 4.56;          // float
const INTEGER_AND_FLOAT = 123 ^ 4.56;    // float

Equality Operators

Used to compare values of all types with each other. The values on either side of the operator must be of the same type.

Equal (==)

This operation returns true if two values are the same and false otherwise.

const TRUE = 123 == 123;      // true
const FALSE = 123 == 456;     // false

Not Equal (!=)

This operation returns true if two values are not the same and false otherwise.

const TRUE = 123 != 456;      // true
const FALSE = 123 != 123;     // false

Last updated