Chapter 2 The DynaScript Language


Operators

This section describes the operators available in scripts.

Ternary, binary and unary operators

Operators act on one or more expressions called operands. Operators can be either ternary, binary or unary: ternary operators act on three expressions, binary operators act on two expressions, and unary operators act on one.

For example, the addition operator is a binary operator, so the following is a valid expression:

x + y ;

The increment operator (which adds one to a number) is a unary operator, so the following is a valid expression:

x++ ;

Arithmetic operators

The following arithmetic operators are provided:

Operator

Description

Binary or unary

+

addition

binary

-

subtraction

binary

*

multiplication

binary

/

division

binary

%

modulo

binary

++

increment

unary

--

decrement

unary

-

negation

unary

Notes

Conditional operator

The following conditional operator is provided:

Operator

Description

?

Conditional

The ? operator is ternary, meaning that it acts on three expressions.

The ? operator evaluates to one of two values, based on a condition. An example of the ? operator is as follows:

(grade == "pass") ? "Excellent" : "Try again";

String operators

The following string operators are provided:

Operator

Description

+

Concatenation

+=

Concatenation with assignment

Comparison operators can also operate on strings, but the result is a Boolean value.

Comparison operators

The following comparison operators are provided:

Operator

Description

= =

Equal to

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

<>

Not equal to

!=

Not equal to

These operators can act on numbers or strings, and return a Boolean (logical) value of true or false .

Boolean operators

Boolean operators operate on Boolean (logical) expressions. The following Boolean operators are provided:

Operator

Name

Description

&&

And

Returns true if both expression are true, false otherwise.

||

Or

Returns true if either expression is true.

!

Not

Returns true if the expression is false, and false if the expression is true.

Bitwise operators

Bitwise operators treat their operands as a set of bits (zeros and ones), rather than as decimal, hexadecimal, or octal numbers. Bitwise operators perform their operations on such binary representations, but they return standard numerical values.

The following bitwise operators are provided:

Operator

Name

Description

&

AND

Returns a one if both operands are ones.

|

OR

Returns a one if either operand is one.

^

XOR

Returns a one if one but not both operands are one.

<<

Left shift

Shifts the first operand the specified number of bits to the left. Excess bits shifted off to the left are discarded. Zero bits are shifted in from the right.

>>

Sign-propagating Right shift

Shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Copies of the leftmost bit are shifted in from the left.

>>>

Zero-fill right shift

Shifts the first operand the specified number of bits to the right. Excess bits shifted off to the right are discarded. Zero bits are shifted in from the left.

Delete operator

The delete operator may be used to delete a property from an object or to delete an element from an array. For example

delete x

Returns true if deletion is successful, false otherwise.

Void operator

The void operator may be used to prevent an expression from returning a value. For example

void addVar

The void operator will evaluate its expression and then returned undefined .

Typeof operator

The typeof operator may be used to return the datatype of an expression. For example

document.writeln( typeof(addVar) );

The typeof operator returns a string that can be one of number, string, Boolean, object, function or undefined.

 


Copyright © 1999 Sybase, Inc. All rights reserved.