[Previous] [Table of Contents] [Next]


In JScript, a simple expression is any part of a program statement that can be interpreted as a single value. JScript allows you to create complex expressions by combining simple expressions using one or more operators. JScript also supports several types of operators: the assignment operators, comparison operators, calculation operators, increment and decrement operators, and logical operators. In this section, you'll find out how to use these operators effectively in your scripts.

Assignment Operator

Earlier, we used the assignment operator (=) in a variable definition. The following statement declares a variable and assigns the value 17:

var tax = 17;

In the upcoming section "Increment and Decrement Operators," you'll find out how to combine the assignment operator with other operators (as in +=).

Comparison Operators

The if statement uses comparison operators (for example, to check two values), which return a Boolean value (true or false). The JScript comparison operators are listed in Table 5-2.

Table 5-2 Comparison Operators

== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
< Less than
> Greater than

The following statement uses a comparison operator:

if (tax == 17) flag = 1;

If the variable tax is equal to 17, the variable flag is set to 1.

The operators = = and != sometimes cause an automatic type conversion. If you want to suppress automatic type conversion, you must write the operators as = = = and ! = =.

Calculation Operators

The simplest calculation operator is the + operator (which we used earlier). The following statements use the calculation operators +, -, *, and /:

var price = 10 + 1;
end_price = net * (1.0 + tax);
net = price - discount;
var res = 100 / 25;

JScript follows the commonly used rules for evaluating expressions with several operators. (The operators * and / have higher priority than + and -.) But you can use parentheses to group subexpressions, which are evaluated first. The JScript calculation operators are listed in Table 5-3.

Table 5-3 Calculation Operators

Operator Description Example
+ Addition a = a + b
- Subtraction a = a - b
* Multiplication a = a * b
/ Division a = a / b
% Modulo division a = a % b

You can combine these operators with the assignment operator = (as you can in the C programming language). It is valid, for example, to write += (as you'll see in upcoming samples).

You must use the + operator for string concatenation in JScript (as in var name = "Günter" + " Born";). This requirement is different from VBScript, in which it's better to use the & operator for concatenation (even though the + operator works). JScript doesn't have a built-in exponentiation operator, such as the VBScript ^ operator. Instead, JScript provides this functionality through the Math.pow(base, exponent) method.

Increment and Decrement Operators

To add or subtract 1 from a variable, you can use the increment operator (++) or the decrement operator (--), respectively; Table 5-4 lists these and other JScript incrementation operators.

Table 5-4 Increment and Decrement Operators

Operator Description
++i, i++ Increments i by 1
--i, i-- Decrements i by 1
+= Addition incrementation
-= Subtraction incrementation
*= Multiplication incrementation
/= Division incrementation
%= Modulo division incrementation

The operators in Table 5-4 are unfamiliar to many Pascal and Basic programmers but are much loved by C programmers because they save you time when you write code. I recommend, however, that you use the more familiar operators to assign and add a value (as in i = i + 1). The following two columns contain equivalent code; the statement in the left column uses the increment or decrement operator, and the statement or statements in the right column use the standard calculation operators:

a += b; a = a + b;
a -= b; a = a - b;
a *= b; a = a * b;
a /= b; a = a / b;
a %= b; a = a % b;
a = ++i; i = i + 1; a = i;
a = i++ a = i; i = i + 1;
a = --i i = i - 1; a = i;
a = i-- a = i; i = i - 1;

The increment and decrement operators also come in handy within loops.

The position of the ++ or -- operator determines when the value is incremented or decremented. In the statement a = ++b;, the ++ operator precedes a variable, which means that the variable is incremented before the rest of the expression is evaluated. In this case, b is incremented and then its value is assigned to the variable a. In the statement a = b++;, the increment operator follows the variable, which means that the expression is evaluated before the increment operator is applied. In the second example, the value of b is assigned to a, and then the value of b is incremented.

Logical Operators

Sometimes you need logical operators (in bit operations, for example). Table 5-5 lists the logical operators that JScript supports.

Table 5-5 Logical Operators

Operator Description
&& Logical And
|| Logical Or
! Logical Not
>> Bits shift right
<< Bits shift left
>>> Unsigned bits shift right
& Bitwise And
| Bitwise Or
~ Bitwise Not
^ Bitwise exclusive Or (Xor)

See Chapter 4 for a discussion of how the bit operators And, Or, and Xor work in VBScript.

Operator Precedence

If a statement contains several operators that aren't set in parentheses, JScript follows a predefined precedence list. The operator precedences are shown in Table 5-6, listed in ascending order.

Table 5-6 Operator Precedence in JScript

Comma ,
Assignment = += _= *= /= %= <<= >>= >>>= &= ^= |=
Conditional ? :
Logical Or ||
Logical And &&
Bitwise Or |
Bitwise Xor ^
Bitwise And &
Equal to, not equal to == != === !==
Relational < <= > >=
Bitwise shift << >> >>>
Addition, subtraction, string concatenation + - +
Multiplication, division * / %
Negation, increment ! ~ - ++ --
Call, member () [] .