Expressions and operators in JavaScript follow closely their cousins in C/C++ or Java. Users familiar with these languages should feel right at home.
Expressions are combinations of variables and operators to produce a resulting value. Expressions can be used as the right hand side of an assignment, as function arguments, and in return statements. Expressions are the heart of JavaScript.
Embedded JavaScript implements a subset of the ECMAScript specification for expressions and operators. The following operators are not supported: ===, !==, >>>, prefix ++ and --, &, |, ^, ?:. The operator / assignment combinations such as *= are also not supported.
Operator Summary
The following table summarises the operators available in Embedded JavaScript. 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
|
Post-increment number
|
--
|
Numbers
|
Post-decrement number
|
<<
|
Integers
|
Left shift
|
>>
|
Integers
|
Right shift
|
== !=
|
All
|
Compare if equal, not equal
|
< <=
|
Numbers, Strings
|
Compare if less than, less than or equal
|
> >=
|
Numbers, Strings |
Compare if greater than, greater than or equal
|
&&
|
Booleans
|
Logical AND
|
||
|
Booleans
|
Logical OR
|
.
|
object.property
|
Property access
|
[]
|
array[integer]
array[string]
|
Array element access
|
()
|
function(any, ...)
|
Function call
|
new
|
constructor function
|
Create a new object using the given constructor
|
delete
|
delete object.property
|
Undefine a property
|
Operator Precedence
When expressions are evaluated, the operands bind to the operators depending on the precedence of the operators. For example:
a = x + y * z;
This is equivalent to:
a = x + (y * z);
The following table lists the operators in order of highest to lowest precedence.
Operator
|
. [] () new
|
++ -- - + delete
|
* / %
|
+ -
|
<< >>
|
< <= > >=
|
== !=
|
&&
|
||
|
=
|
,
|
Operators
Addition
The + operator will add numeric operands (integers and floats). If used with string operands it will catenate the strings. If the left hand operand is a string, the right hand operand will be converted to a string. If the right hand operand is an object, it will have its toString method invoked to convert its value to a string.
Subtraction, Multiplication, Division, Modulo
If used with non-numeric operands, they will be converted to numbers first. If one operand is floating point, the other will be converted to be floating also.
Equality, Inequality
The ECMA JavaScript standard treats the == and === operators differently. Embedded JavaScript only implements the == operator. The == operator will compare its operands for equality after performing any required type conversions according to the following procedure:
- If they are primitive types and are both of the same type, their values are compared
- If they are not both of the same type, EJS will use type conversion first before comparing.
- If one is undefined and the other is null, they are regarded as being NOT equal.
See
Type Conversions for more details about how type conversions are implemented.
Less Than, Greater Than
String comparisions are performed according to the ASCII collating sequence.
Delete
The delete operator removes the specified propery from the referenced object. For example:
delete customer.currentOrder;
This will delete the currentOrder property from the customer object.