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

String operators

These operators are used to perform operations on strings of characters.

"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 operator

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

const a = null
const b = undefined
a ?? b ?? 123 // outputs 123

You may also be interested in the OrDefault intrinsic function, which returns the first non-undefined value.

const abc = undefined
abc.OrDefault("123") // outputs "123"

const xyz = null
xyz.OrDefault("456") // outputs null

Optional chaining operator

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

const 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.

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