Operators

What is an Operator?

Simple answer can be given using expression 4 + 5 is equal to 9. Here, 4 and 5 are called operands and +

is called operator. VBScript language supports following types of operators:

The Arithmatic Operators

There are following arithmatic operators supported by VBScript language:

Assume variable A holds 5 and variable B holds 10, then:

+

Adds two operands

Example

A + B will give 15

-

Subtracts second operand from the first.

Example

A - B will give -5

*

Multiply both operands.

Example

A * B will give 50

/

Divide numerator by denumerator.

Example

B / A will give 2

%

Modulus Operator and remainder of after an integer division.

Example

B MOD A will give 0

^

Exponentiation Operator.

Example

B ^ A will give 100000

Example: Arithmetic Operators in use

Try the following example to understand all the arithmetic operators available in VBScript:

Dim a
Dim b
Dim Result
 
a = 5
b = 10

Result = a + b

Result

When the above code is executed the value of the Result variable is the integer: 15

The Comparison Operators

There are following comparison operators supported by VBScript language:

Assume variable A holds 10 and variable B holds 20, then:

=

Checks if the value of two operands are equal or not, if yes then condition becomes true.

Example

(A = B) is False.

<>

Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

Example

(A <> B) is True.

>

Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

Example

(A > B) is False.

<

Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

Example

(A < B) is True.

(A >= B) is False.

>=

Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

<=

Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

Example

(A <= B) is True.

Example: Comparison Operators in Use

Dim a
Dim b
Dim Result
 
a = 5
b = 10

Result = a > b

Result

When the above code is executed the value of the Result variable is 'False' (a is less than b).

The Logical Operators:

There are following logical operators supported by VBScript language:

Assume variable A holds 10 and variable B holds 0, then:

And

Called Logical AND operator. If both the conditions are True then Expression becomes true.

a<>0 AND b<>0 is False.

Or

Called Logical OR Operator. If any of the two conditions are True then condition becomes true.

a<>0 OR b<>0 is true.

Not

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

NOT(a<>0 OR b<>0) is false.

Xor

Called Logical Exclusion. It is the combination of NOT and OR Operator. If one, and only one, of the expressions evaluates to True, result is True.

(a<>0 XOR b<>0) is false.

Example

Dim a
Dim b
Dim Result
 
a = 5
b = 10

Result = a <> 0 And b > 5

Result

When the above code is executed the value of the Result variable is 'False' (a is less than b).

The Concatenation Operators

There are two Concatenation operators supported by VBScript language: + and &.

AMPERSAND Concatenation Operator (&)

Example

Where A = 5 and B = 10

A & B will give 510

Example: Concatenation Operators in Use

Try the following example to understand the Concatenation operator available in VBScript:

Dim Val1
Dim Val2
Dim Result
 
Val1 = 5
Val2 = "ten"

Result = Val1 & Val2

Result

When the above code is executed the value of the Result variable is 5ten.

Dim Val1
Dim Val2
Dim Result
 
Val1 = 5
Val2 = 10

Result = Val1 & Val2

Result

When the above code is executed the value of the Result variable is 510.

PLUS Concatenation Operator (+)

Example

Where A = "five" and B = "ten"

A + B will give fiveten

Whereas A = 5 and B = 10

A + B will give 15

Concatenation Operators can be used for numbers and strings. The Output depends on the context if the variables hold numeric value or String Value. We highly recommend using ampersand (&) for concatenation as plus (+) can lead to unintended results.

Example: Concatenation Operators in Use

Example 1 - Concatenating Strings

Try the following example to understand the Concatenation operator available in VBScript:

Dim Val1
Dim Val2
Dim Result
 
Val1 = "string1"
Val2 = "string2"

Result = Val1 + Val2

Result

When the above code is executed the value of the Result variable is string1string2.

Example 2 - Concatenating Numeric-ish Values

Dim Val1
Dim Val2
Dim Val3
Dim Result
 
Val1 = 5
Val2 = 10
Val3 = "3"

Result = Val1 + Val2 + Val3

Result

When the above code is executed the value of the Result variable is 18. Despite Val3 being a string it is treated like a number.

Example 3 - Concatenating mixed numeric and strings

When the above code is executed the value of the Result variable is string1string2.

Dim Val1
Dim Val2
Dim Val3
Dim Result
 
Val1 = 5
Val2 = 10
Val3 = "three"

Result = Val1 + Val2 + Val3

Result

When the above code is executed the value of the Result variable is 510three.