VBScript Operators

VBScript has a full range of operators. There are four different types operators in VBScript:

  1. Arithmetic Operators
  2. Comparison Operators
  3. Concatenation Operators
  4. Logical Operators

Operator Precedence: When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. When expressions contain operators from more than one category –

  • Arithmetic Operators are evaluated first.
  • Comparison Operators are evaluated next.
  • Logical Operators are evaluated last.

Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear.

1. Arithmetic Operator

Arithmetic Operators are used to perform mathematical operations like addition, subtraction, multiplication, and other mathematical operations on numbers and variables. Different Arithmetic operators we have in VBScript are:

Multiplication *
Division /
Addition +
Subtraction
String Concatenation & (Combine two different data types)
Integer Division \
Modulus Arithmetic Mod
Exponentiation ^
Unary Negation

Arithmetic operators are evaluated in the following order of precedence.
BODMAS – Bracket Order of Division Multiplication Addition Subtraction.

When multiplication and division occur together in an expression, each operation is evaluated as it occurs from left to right. Likewise, when addition and subtraction occur together in an expression, each operation is evaluated in order of appearance from left to right.

2. Comparison Operator

Comparison operators are used to compare different numbers/strings and are mostly used in conditional statements such as If condition or While Loop.

When we want to compare two numbers/string to check if a number/string is equal to another, checks if a number is greater or less than the other or some other type of relationship use the comparison operators. Different Comparison operators we have in VBScript are:

Equality =
Inequality <>
Less than <
Greater than >
Less than or Equal to <=
Greater than or Equal to >=
Object Equivalence IS

Example:
When comparing two expressions, we may not be able to easily determine whether the expressions are being compared as numbers or as strings.

  • If Both expressions are numeric Then Perform a numeric comparison.
  • If Both expressions are strings Then Perform a string comparison.
  • If One expression is numeric and the other is a string Then The numeric expression is less than the string expression.
  • If One expression is Empty and the other is numeric Then Perform a numeric comparison, using 0 as the Empty expression.
  • If One expression is Empty and the other is a string Then Perform a string comparison, using a zero-length string (“”) as the Empty expression.
  • If Both expressions are Empty Then The expressions are equal.

The IS operator is an object reference comparison operator. It does not compare objects or their values; it checks only to determine if two object references refer to the same object.

3. Concatenation Operator

Concatenation operator is used to join various strings. There is only one concatenation operator & (ampersand).

  • When we have various strings that we would like to combine into one string use the concatenation operator.
  • The string concatenation (&) operator is not an arithmetic operator, but in precedence it falls after all arithmetic operators and before all comparison operators.

4. Logical Operator

Logic operators are used to manipulate and create logical statements. VBScript logical operators allow you to combine operations done by comparison operators to make “compound” If statements, among other things.

Logical operators take in two (or one) Boolean values and return another Boolean value.

Logical Conjunction AND (Both Condition has to be True)
Logical Disjunction OR (Any condition has to be True)
Logical Negation NOT
Logical Exclusion XOR
Logical Equivalence EQV
Logical Implication IMP

AND – Performs a logical conjunction on two expressions (if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False). For Example,

OR – Performs a logical disjunction on two expressions (if either or both expressions evaluate to True, result is True). For Example,

NOT – Performs logical negation on an expression. The logical Not operator results in the opposite Boolean value of the single operand. Unlike other operators, Not only takes one operator to its right (it is a “unary” operator).

XOR – Performs a logical exclusion on two expressions (if one, and only one, of the expressions evaluates to True, result is True. However, if either expression is Null, result is also Null).

EQV – The logical Eqv operator (“equivalence”) results in True when both operands are True or both operands are False. If one operand is True and the other is False, Eqv returns False.

IMP – The logical Imp operator results in True in all cases when the second operand is True and when both operands are False. It results in False only when the first operand is True and the second is False.

About the Author

Leave Comment