Skip to main content

Operators

Operators are used to perform operations on values.

Arithmetic operators​

These operators are used to perform arithmetic operations on values.

1 + 2 // addition - outputs 3
2 - 1 // subtraction - outputs 1
2 * 3 // multiplication - outputs 6
6 / 2 // division - outputs 3
5 % 2 // modulus - outputs 1
2 ** 3 // exponentiation - outputs 8
info

Dividing by zero in Easel returns undefined.

String operators​

Strings may be concatenated using the + operator.

"Hello " + "world" // concatenation - outputs "Hello world"

Relational operators​

These operators are used to compare values.

1 == 1 // equality - outputs true
1 != 1 // inequality - outputs false
1 < 2 // less than - outputs true
1 <= 2 // less than or equal to - outputs true
1 > 2 // greater than - outputs false
1 >= 2 // greater than or equal to - outputs false

Logical operators​

These operators are used to combine boolean values.

!true // logical NOT - outputs false
true && false // logical AND - outputs false
true || false // logical OR - outputs true
true ^ true // logical XOR - outputs false

Bitwise operators​

These operators are used to perform bitwise operations on values.

~0b010 // bitwise NOT - outputs 0b101

0b110 & 0b010 // bitwise AND - outputs 0b010
0b100 | 0b010 // bitwise OR - outputs 0b110
0b110 ^ 0b010 // bitwise XOR - outputs 0b100

0b010 << 1 // bitwise left shift - outputs 0b100
0b010 >> 1 // bitwise right shift - outputs 0b001

Coalescing operators​

The null coalescing operator ?? is used to return the first non-null value.

let a = null
let b = undefined
let output = a ?? b ?? 123 // outputs 123

The existential coalescing operator ??? returns the first entity that exists:

let a = Spawn
let b = Spawn
let c = Spawn
a.Despawn
let output = a ??? b ??? c // outputs `b`, because `a` no longer exists

Optional chaining operator​

The optional chaining operator returns null if any part of the chain is null or undefined

let vehicle = null
vehicle?.Make?.Model // outputs null

Note that in general, null values will just pass through most intrinsic functions without error, and so optional chaining and coalescing is often not necessary.

Ternary operator​

The ternary operator is used to conditionally return one of two values.

let x = 5
x > 3 ? "greater" : "less" // outputs "greater"

Collection operators​

Concatenate Arrays or Maps using the + operator. This creates a new Array or Map consisting of the elements of the first collection followed by the elements of the second collection.

[1, 2] + [3, 4] // outputs [1, 2, 3, 4]
info

If you want to add elements to an existing collection, it is more efficient to use the Push or PushAll functions, as they will reuse the existing collection.