Expressions
The topics are:
Script Expressions
Expressions are a combination of literals, variables, special keywords, and operators.
Note
For C/C++ programmers: The syntax of Script expressions is very close to the C/C++ syntax. |
The precedence of operators determines the order in which they are applied when evaluating an expression. Operator precedence can be overridden by using parentheses.
The following table lists the Script operators and gives their precedence, from lowest to highest:
Category |
Operators |
Sequence |
, |
Assignment |
= += -= *= /= %= <<= >>= >>>= &= ^= |= |
Conditional |
?: |
Logical-or |
| | |
Logical-and |
&& |
Bitwise-or |
| |
Bitwise-xor |
^ |
Bitwise-and |
& |
Equality |
== != |
Relational |
< <= > >= |
Bitwise shift |
<< >> >>> |
Addition, substraction |
+ - |
Multiply, divide |
* / % |
Negation, increment, typeof |
! ~ - ++ -- typeof |
Call |
( ) |
New |
new |
Property |
. [ ] |
Literals
Literals can represent:
-
Numbers , for example: 12 14.5 1.7e-100
-
Strings , for example: "Ford" "Hello world\n"
-
Booleans , either true or false.
-
The null Value : null.
See Number Literal Syntax and String Literal Syntax for further details about number and string literal syntax.
Variable Reference
Variable reference syntax is shown in the following table.
Syntax |
Effect |
variable |
Returns the value of variable. See Identifier Syntax for the syntax of variables. If variable doesn't exist, an error is signalled. This is not the same as referencing an existing variable whose value is the The undefined Value —which is legal and returns the undefined value. When used in the body of a Default Value statement, a variable reference is first looked up as a property of the current default value. |
Property Access
There are two syntaxes for accessing a value property:
Syntax |
Effect |
value.name |
Returns the value of the name property of value, or the The undefined Value if this property is not defined. See Identifier Syntax for the syntax of name. Examples: str.length Because name must be a valid identifier, this form cannot be used to access properties which don't have a valid identifier syntax. For example, the numeric properties of an Arrays cannot be accessed this way: myArray.10 // Illegal syntax For these properties, use the second syntax. |
value[name] |
Same as the previous syntax, except that this time name is an evaluated expression which yields the property name. Examples: str["length"] // Same as str.length getCar()[getPropertyName()] myArray[10] myArray[i+1] |
Assignment Operators
The = operator can be used to assign a new value to a variable or a property:
Syntax |
Effect |
Assigns the value of expression to variable. If variable does not exist, it is created as a global variable. Examples: x = y+1 The whole expression returns the value of expression. |
|
value.name = expression value[name] = expression |
Assigns the value of expression to the given property. If value doesn't have such a property, then if it is either an Arrays or an Objects , the property is created; otherwise, an error is signalled. Examples: car.name = "Ford" myArray[i] = myArray[i]+1 The whole expression returns the value of expression. |
In addition, the following shorthand operators are also defined:
Syntax |
Shorthand For |
++X |
X = X+1 |
X++ |
Same as ++X, but returns the initial value of X instead of its new value. |
--X |
X = X-1 |
X-- |
Same as --X, but returns the initial value of X instead of its new value. |
X += Y |
X = X + Y |
X -= Y |
X = X - Y |
X *= Y |
X = X * Y |
X /= Y |
X = X / Y |
X %= Y |
X = X % Y |
X <<= Y |
X = X << Y |
X >>= Y |
X = X >> Y |
X >>>= Y |
X = X >>> Y |
X &= Y |
X = X & Y |
X ^= Y |
X = X ^ Y |
X |= Y |
X = X | Y |
Function Call
The syntax for calling a function is:
Syntax |
Effect |
function(arg1, ..., argn) |
Calls function with the given arguments, and returns the result of the call. Examples: parseInt(field) Function is typically either a variable reference or a property access, but it can be any arbitrary expression; the expression must yield a Functions , or an error is signalled. Examples: // Calls the function in callbacks[i] |
Special Keywords
Special keywords that can be used are:
Syntax |
Effect |
When referenced in a method, returns the current calling object; when referenced in a constructor, returns the object currently being initialized. Otherwise, returns the global object. See Objects for examples. |
|
Returns an Arrays containing the arguments of the current function. When used outside of a function, an error is signalled. For example, the following function returns the sum of all its arguments: function sum() { The call sum(1, 3, 5) returns 9. |
Special Operators
The special operators are:
Syntax |
Effect |
|
Calls the constructor with the given arguments, and returns the created value. Examples: new Array() new MyCar("Ford", 1975) Constructor is typically a variable reference, but it can be any arbitrary expression. Example: new ctors[i](arg) // Invokes constructor ctors[i] |
||
Returns a string representing the type of value, as follows: |
||
Type of value |
Result of typeof value |
|
"object" |
||
"boolean" |
||
"date" |
||
"function" |
||
"object" |
||
"number" |
||
"object" |
||
"string" |
||
"undefined" |
||
Delete the global variable variable. This doesn't mean that the value in variable is deleted, but that variable is removed from the global environment. Example: myVar = "Hello, world" // Create the global variable myVar If variable is a local variable, an error is signalled; if variable is not a known variable, nothing happens. The whole expression returns the Booleans . Note for C/C++ programmers: This operator has a radically different meaning than in C++, where it is used to delete objects, not variables and properties. |
||
delete value[name] |
Remove the property name from the Objects value. If value doesn't contain the name property, this expression does nothing. If the property does exist but cannot be deleted, an error is signalled. If value is not an object, an error is signalled. The whole expression returns the Booleans . |
|
Evaluates sequentially expression1 and expression2, and returns the value of expression2. The value of expression1 is ignored. The most common use for this operator is inside for loops, where it can be used to evaluate several expressions where a single expression is expected: for (var i=0, j=0; i<10; i++, j+=2) { |
Other Operators
Other operators are described in the section dedicated to the datatype they operate on. They are:
Syntax |
Effect |
- X |
These operators perform the usual arithmetic operations. In addition, the + operator can be used to concatenate strings. See Numeric Operators and String Operators . |
X == Y |
These operators can be used to compare numbers and strings; see Numeric Operators and String Operators . For other types of values, such as Dates , Arrays , Objects , and so forth, the == operator is true if, and only if, X and Y are the exact same value. For example: new Array(10) == new Array(10) -> false var a = new Array(10); a == a -> true |
X > Y |
Relational operators. These operators can be used to compare numbers and strings. See Numeric Operators and String Operators . |
~ X |
See Numeric Operators . |
! X |
See Logical Operators . |