Expressions and Operators
Expressions and operators in Ejscript and ECMAScript follow closely their cousins in C and Java. Users familiar with these languages should feel right at home.
Expressions are combinations of variables and operators that evaluate to a single value. Expressions can be used as the right hand side of an assignment, as function arguments, and in return statements.
Operator Summary
The following table summarizes the operators available in Ejscript. For a complete specification, please consult the ECMA-262 specification.
Operator | Operands | Description |
---|---|---|
* / % | Numbers | Multiply, divide, modulo |
+ | Numbers, Strings | Add numbers, catenate strings |
- | Numbers | Subtract numbers |
! | Boolean | Unary not |
++ | Numbers | Pre/Post-increment number |
-- | Numbers | Pre/Post-decrement number |
<< | Integers | Left shift |
>> | Integers | Right shift |
== != | All | Compare if equal, not equal |
=== !== | All | Strict equality, inequality. Object references must equal. |
< <= | Numbers, Strings | Compare if less than, less than or equal |
> >= | Numbers, Strings | Compare if greater than, greater than or equal |
& | Numbers | Bitwise AND |
| | Numbers | Bitwise OR |
&& | Booleans | Logical AND |
|| | Booleans | Logical OR |
. | object.property | Property access |
[] | array[integer]
array[string] |
Array, Object, ByteArray element access |
() | function(any, ...) | Function call |
cast | Any | Cast the operand to the required type |
is | Any | Test if the operand is the required type |
in | Any | Test if name is a property of the target object |
is instanceof like | Any | Test if the operand is of the required type |
The following operators can also be combined with assignment to provide a compound operator: * / % + - ! << >> & | && and ||. For example:
x += 2
Ejscript uses a type-less VM where the types themselves implement the operators. Users can define new native types which implement any of the operators on their native type.
Operator Precedence
When expressions are evaluated, the operands bind to the operators depending on the precedence of the operators.
The following table lists the operators in order of highest to lowest precedence. Operators of the same precedence share the same row in the table:
Operator |
---|
. [] () new |
++ -- - + delete |
* / % |
+ - |
<< >> |
< <= > >= cast in instanceof in is like |
== != |
&& |
|| |
= |
, |
For example, the expression: a = x + y * z is equivalent to a = x + (y * z).