Chapter 2 The DynaScript Language
This section describes the operators available in scripts.
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 this is a valid expression:
x + y ;
The increment operator (which adds one to a number) is a unary operator, so this is also a valid expression:
x++ ;
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 |
x++ ++x
x = 56 ; y = x-- ; // Sets y to 56 and decrements x to 55 y = --x ; // Decrements x to 55 and sets y to 55
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";
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.
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 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 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. |
The delete operator can 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.
The void operator can be used to prevent an expression from returning a value. For example:
void addVar
The void operator evaluates its expression
and then return undefined
.
The typeof operator can 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 © 2001 Sybase, Inc. All rights reserved. |
![]() |