VBScript operators allow you to compare values, do math with values, or put values together. They perform operations on values. Many VBScript operators perform different things based on context.
In VBScript, operators perform operations to two operands (one operand in the case of the
Not operator), many times the resulting action can depend on the context or content of operands.
Operators are evaluated in the "order of operations", similar to what you may have learned in math class.
Order of operations
The order of operation is the order that operations are done. Operators of the same type act from left to right, but of different type act depending on an order that was meant to make the most sense. Use parentheses (
( and </code>)</code>) to force certain operations to occur before others.
Operators with a higher precedence number are evaluated first.
||Operations in parentheses|
||Object member access, and other function, property, and variable evaluation|
||Multiplication, division (decimal), division (integer), modulus|
||Equal to, greater than, less than, greater than or equal to, less than or equal to, not equal to, object references equal to|
||Logical or bitwise negation|
||Logical or bitwise conjunction|
||Logical or bitwise disjunction|
||Logical or bitwise exclusion|
||Logical or bitwise equivalence|
||Logical or bitwise implication|
Operations in parentheses are the highest precedence so that you can use them to evaluate specific parts of an expression before other parts, similar to their meaning in mathematical expressions.
Argument lists in functions are technically in parentheses so the set of arguments being passed to a function or subroutine are evaluated before the function processes them.
Assignment is always the last evaluated operator, and trying to use parentheses to change this will result in errors.
Value = CreateObject("Scripting.Dictionary").Count
Here, the CreateObject() function will return a new dictionary, then the result will be accessed for the
Count property which will then be assigned to
Value. This will not try to assign the object to
If 1 + 2 * 3 ^ 4 <> ((1 + 2) * 3) ^ 4 Then
This if block will return True, illustrating that the mathematical operations will be evaluated first, which goes in order: exponent, multiplication, addition; then the inequality will be evaluated.
The Assignment operator,
=, allows you to assign the variable, value, or result of another operation or function on the right to the variable on the left.
Example: Store a literal value into
Var1 = "a literal string value"
Example: Store the value of
Var1 = Var2
Example: Store the result of a function into
SomeFunction with the variable
SomeArgument passed to it.
Var1 = SomeFunction(SomeArgument)
& operator takes two values and returns the result of them as a String.
Var1 = "H" & "ello"
Var1 will contain the string "Hello".
Var1 = "The number is " & 1
Var1 will contain the string "The number is 1".
Var1 = 1 & 1
Var1 will contain the string "11" even though neither operands are a string. In all cases, a variable or function call could be substituted for the literals in these examples.
It is also valid to use the
+ operator between two string values to concatenate them, but you have to make sure one operator is string or it will attempt to add the values.
- Main article: VBScript comparison operator
Comparison operators compare two operands and return a Boolean indicating success (True) or failure (False).
- Main article: VBScript logical operator
Logical operators are a type of comparison operator that takes two Booleans and returns another Boolean, such as the results of other comparison operators.
- Main article: VBScript arithmetic operator
Arithmetic operators take two numeric operands and return a Double result.
- Main article: VBScript bitwise operator
Bitwise operators are a type of arithmetic operator that takes two Integers and returns another Integer. In VBScript, the same operators that are logical operators are also bitwise operators. "Bitwise" meaning that they operate on each "bit" or part of the number in binary, basically doing the same operation as their logical counterpart but to each of the bits.