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:

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Concatenation 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:

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

   Dim a : a = 5

   Dim b : b = 10

   Dim c     c = a+b

   Document.write ("Addition Result is " &c)

   Document.write ("<br></br>")    'Inserting a Line Break for readability    c = a-b

   Document.write ("Subtraction Result is " &c)

   Document.write ("<br></br>")   'Inserting a Line Break for readability    c = a*b

   Document.write ("Multiplication Result is " &c)

   Document.write ("<br></br>")

   c = b/a

   Document.write ("Division Result is " &c)

   Document.write ("<br></br>")

   c = b MOD a

   Document.write ("Modulus Result is " &c)

   Document.write ("<br></br>")

   c = b^a

   Document.write ("Exponentiation Result is " &c)

   Document.write ("<br></br>")

</script>

</body>

</html>

When you save it as .html and execute it in Internet Explorer, then the above script will produce the following result:

  • Addition Result is 15
  • Subtraction Result is -5
  • Multiplication Result is 50
  • Division Result is 2
  • Modulus Result is 0
  • Exponentiation Result is 100000

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

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

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

  Dim a : a = 10

  Dim b : b = 20

  Dim c

  If a=b Then 

Document.write ("Operator Line 1 : True")

Document.write ("<br></br>")  'Inserting a Line Break for readability   Else

Document.write ("Operator Line 1 : False")

Document.write ("<br></br>")  'Inserting a Line Break for readability

  End If

  If a<>b Then

Document.write ("Operator Line 2 : True")

Document.write ("<br></br>")

  Else

Document.write ("Operator Line 2 : False")

Document.write ("<br></br>")

  End If

  If a>b Then

Document.write ("Operator Line 3 : True")

Document.write ("<br></br>")

  Else

Document.write ("Operator Line 3 : False")

Document.write ("<br></br>")

  End If

  If a<b Then

Document.write ("Operator Line 4 : True")

Document.write ("<br></br>")

  Else

Document.write ("Operator Line 4 : False")

Document.write ("<br></br>")

  End If

  If a>=b Then

Document.write ("Operator Line 5 : True")

Document.write ("<br></br>")

  Else

Document.write ("Operator Line 5 : False")

Document.write ("<br></br>")

  End If

  If a<=b Then

Document.write ("Operator Line 6 : True")

Document.write ("<br></br>")

  Else

Document.write ("Operator Line 6 : False")

Document.write ("<br></br>")

  End If

</script>

</body>

</html>

When you save it as .html and execute it in Internet Explorer, then the above script will produce the following result:

  • Operator Line 1 : False
  • Operator Line 2 : True
  • Operator Line 3 : False
  • Operator Line 4 : True
  • Operator Line 5 : False
  • Operator Line 6 : True

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

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

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

  Dim a : a = 10

  Dim b : b = 0 

  Dim c

  If a<>0 AND b<>0 Then                    

     Document.write ("AND Operator Result is : True")

     Document.write ("<br></br>")  'Inserting a Line Break for readability

  Else

     Document.write ("AND Operator Result is : False")

     Document.write ("<br></br>")  'Inserting a Line Break for readability

  End If

  If a<>0 OR b<>0 Then

     Document.write ("OR Operator Result is : True")

     Document.write ("<br></br>")

  Else

     Document.write ("OR Operator Result is : False")

     Document.write ("<br></br>") 

  End If

  If NOT(a<>0 OR b<>0) Then

     Document.write ("NOT Operator Result is : True")

     Document.write ("<br></br>") 

  Else

     Document.write ("NOT Operator Result is : False")

     Document.write ("<br></br>") 

  End If

  If (a<>0 XOR b<>0) Then

     Document.write ("XOR Operator Result is : True")

     Document.write ("<br></br>") 

  Else

     Document.write ("XOR Operator Result is : False")

     Document.write ("<br></br>") 

  End If

</script>

</body>

</html>

When you save it as .html and execute it in Internet Explorer, then the above script will produce the following result:

  • AND Operator Result is : False
  • OR Operator Result is : True
  • NOT Operator Result is : False
  • XOR Operator Result is : True

To understand these operators in a better way, you can Try it yourself.

The Concatenation Operators

There are following Concatenation operators supported by VBScript language:

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

+

Adds two Values as Variable Values are Numeric.

Example

A + B will give 15

&

Concatenates two Values.

Example

A & B will give 510

Example 1: Concatenation Operators in Use

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

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

  Dim a : a = 5

  Dim b : b = 10

  Dim c    c=a+b 

  Document.write ("Concatenated value:1 is " &c) 'Numeric addition    Document.write ("<br></br>")  'Inserting a Line Break for readability   c=a&b 

  Document.write ("Concatenated value:2 is " &c) 'Concatenate two numbers 

  Document.write ("<br></br>")  'Inserting a Line Break for readability

</script>

</body>

</html>

When you save it as .html and execute it in Internet Explorer, then the above script will produce the following result:

  • Concatenated value:1 is 15
  • Concatenated value:2 is 510

Assume variable A="Microsoft" and variable B="VBScript", then:

+

Concatenates two Values.

Example

A + B will give MicrosoftVBScript

&

Concatenates two Values

Example

A & B will give MicrosoftVBScript

Example 2: Concatenation Operators in Use

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

<!DOCTYPE html>

<html>

<body>

<script language="vbscript" type="text/vbscript">

  Dim a : a = "Microsoft"

  Dim b : b = "VBScript"

  Dim c    c=a+b 

  Document.write ("Concatenated value:1 is " &c) 'Numeric addition    Document.write ("<br></br>")  'Inserting a Line Break for readability   c=a&b 

  Document.write ("Concatenated value:2 is " &c) 'Concatenate two numbers 

  Document.write ("<br></br>")  'Inserting a Line Break for readability

</script>

</body>

</html>

When you save it as .html and execute it in Internet Explorer, then the above script will produce the following result:

  • Concatenated value:1 is MicrosoftVBScript
  • Concatenated value:2 is MicrosoftVBScript
Concatenation Operators can be used for numbers and strings. The Output depends on the context if the variables hold numeric value or String Value.