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:

Script Operator Precedence

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"

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.

Script Variable Syntax

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:

Script Property Access Syntax

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

getCar().name

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:

Script Assignment Operator Syntax

Syntax

Effect

variable = expression

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:

Shorthand Operators

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:

Script Function Call Syntax

Syntax

Effect

function(arg1, ..., argn)

Calls function with the given arguments, and returns the result of the call.

Examples:

parseInt(field)

writeln("Hello ", name)

doAction()

str.substring(start, start+length)

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]
callbacks[i](arg)

// Error: a string is not a function
"foo"()

Special Keywords

Special keywords that can be used are:

Script Special Keywords

Syntax

Effect

this

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.

arguments

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() {
    var res = 0
    for (var i=0; i<arguments.length; i++)
      res = res+arguments[i]
    return res
  }

The call sum(1, 3, 5) returns 9.

Special Operators

The special operators are:

Script Special Operator Syntax

Syntax

Effect

new constructor(arg1, ..., argn)

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]

typeof value

Returns a string representing the type of value, as follows:

Type of value

Result of typeof value

Arrays

"object"

Booleans

"boolean"

Dates

"date"

Functions

"function"

The null Value

"object"

Numbers

"number"

Objects

"object"

Objects

"string"

The undefined Value

"undefined"

delete variable

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
  delete myVar
  writeln(myVar) // Signals an error because myVar is undefined

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

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 .

expression1 , expression2

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) {
    writeln(j, " is twice as big as ", i); }

Other Operators

Other operators are described in the section dedicated to the datatype they operate on. They are:

Other Script Operators

Syntax

Effect

- X
X
+ Y
X
- Y
X
* Y
X
/ Y
X
% Y

Arithmetic operators.

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
X
!= Y

Equality operators.

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
X
>= Y
X
< Y
X
<= Y

Relational operators.

These operators can be used to compare numbers and strings. See Numeric Operators and String Operators .

~ X
X
& Y
X
| Y
X
^ Y
X
<< Y
X
>> Y
X
>>> Y

Bitwise operators.

See Numeric Operators .

! X
X
|| Y
X
&& Y
condition
? X : Y

Logical operators.

See Logical Operators .