VBScript supports almost all types of operators same as JavaScript or JScript including arithmetic, comparison, concatenation, and logical operators.
I have listed all operators with their example for use below:
Arithmetic operators
Addition
use to calculate the sum of the expressions if expression are integer type. If the expressions are string than Addition Operator will concatenate the expression. See the following example:
Example 1.1
Dim sum1 Dim str sum1 = 4 + 9 str = "Welcome" + " to " + " Authorcode" MsgBox Sum1 MsgBox str
Subtraction
This operator is used to subtract the two numbers. See the example:
Example 1.2
Dim numSub numSub = 9 - 6 MsgBox numSub
Division
Divides two numbers and returns a floating-point result. See the following example:
Example 1.3
Dim num1 Dim num2 num1 = 89 num2 = 5 Dim result resultSum = num1/num2 resultMul = num1*num2 MsgBox result MsgBox resultMul
Multiplication
Multiplies two numbers and returns a floating-point result. See the example 1.3.
Exponentiation
Exponentiation is a mathematical operation that calculate the value of a number to the power of an exponent. Let’s suppose you want to calculate the value of 7 to the power 4 than you can write the VBScript as:
Example 1.4
Dim result resultSum = 7^5 MsgBox result
Mod
In VBScript, we use the mod keyword to get the remainder value. This operator divides two integers and returns the integer as remainder.
Example 1.5
Dim num1 Dim num2 num1 = 89 num2 = 5 Dim result resultSum = num1 mod num2 MsgBox result
Concatenation
This operator concatenate strings using ‘&’ like as:
Example 1.6
Dim str str = "Welcome" & " to " & " Authorcode" MsgBox str
Integer Division
This operator(\) divides two numbers and returns an integer result. It exclude the fraction parts from the numeric expression.
Example 1.7
Dim num1 Dim num2 num1 = 89 num2 = 5 Dim result resultSum = num1\num2 MsgBox result
Comparison operators
Comparison operators is used to compare two expressions and returns the true or false or null value. You can compare expressions in the following ways:
Let’s suppose there are to expression exp1 and exp2.
Less than (<)
expression: exp1 < exp2
Less than or equal to(<=)
expression: exp1 <= exp2
Greater than (>)
expression: exp1 > exp2
Greater than or equal to (>=)
expression: exp1 >= exp2
Equal to(=)
expression: exp1 = exp2
Not equal to (<>)
expression: exp1 <> exp2
Let’s see the example that shows how can we use these operators:
Dim num1 Dim num2 num1 = 78 num2 = 55 If num1 < num2 Than MsgBox "Num1 is less than num2" EndIf If num1 > num2 Than MsgBox "Num1 is greater than num2" EndIf If num1 <> num2 Than MsgBox "Num1 is not equal to num2" EndIf num1 = 55 num2 = 55 If num1 = num2 Than MsgBox "Num1 is equal to num2" EndIf
If both expression are null then than the return value will also be null.
Logical operators
And now we discuss the logical operators.
Logical AND
The following example shows examples of the logical AND operator:
result1 = true AND true ' true AND true returns true
result2 = true AND false ' true AND false returns false
result3 = false AND true ' false AND true returns false
result4 = false AND (3 = 4) ' false AND false returns false
result5 = false AND (3=3) ' false AND true returns false
result6 = (4<5) AND (4<>5) ' true AND false returns false
Logical OR
result1 = true OR true ‘ true OR true returns true
result2 = true OR false ‘ true OR false returns true
result3 = false OR true ‘ false OR true returns true
Logical NOT
The following code shows examples of the NOT (logical NOT) operator.
result1 = Not true ‘ false
result2 = NOT false ‘ true
Next chapter:Looping in VBScript
Previous chapter:Using VBScript in HTML document