Assigning Values to Variables - "=" Statement
This section provides a quick introduction of the assignment statement, which allows you to assign a new value to a variable.
Variables can be assigned with new values with the assignment statement in the following syntax:
variable_name = data_value
where "data_value" a data literal or an expression. We will look at expressions in details in other parts
of this book.
When assigning a new value to a variable:
- The old value in the variable will be removed.
- The new value will be saved into the variable.
- The subtype of new value does not have to match with the subtype of the old value.
Here is a tutorial example on how to declare variables and assign values to them:
- Copyright (c) 2015, HerongYang.com, All Rights Reserved.
' Dim empty
author = "Herong"
price = 9.99
' empty = 0
buffer = price ' Implicit declaration of "buffer"
price = author
author = buffer
document.writeln("author = " & author)
document.writeln("price = " & price)
document.writeln("empty = " & empty)
Here is the output of this VBScript example:
author = 9.99
price = Herong
Notes about this VBScript example:
- Statement, Dim empty, is commented out, because "empty" is a reserved keyword. It can not be used as a variable name.
- Statement, author = "Herong", assigns value "Herong" to variable "author".
- Statement, price = author, assigns the value from variable "author" to variable "price".
Not that the subtype of the new value does not match the subtype of the new value.
Table of Contents
About This Book
Introduction of VBScript - Visual Basic Scripting Edition
Variant Data Type, Subtypes, and Literals
Numeric Comparison Operations and Logical Operations
String Operations - Concatenation and Comparison
►Variable Declaration and Assignment Statement
Variable Declaration and "Dim" Statement
►Assigning Values to Variables - "=" Statement
Empty - The Default Value of a Variable
Expression and Order of Operation Precedence
Statement Syntax and Statement Types
Array Data Type and Related Statements
Array References and Array Assignment Statements
Conditional Statements - "If ... Then" and "Select Case"
Loop Statements - "For", "While", and "Do"
"Function" and "Sub" Procedures
Inspecting Variables Received in Procedures
Error Handling Flag and the "Err" Object
Regular Expression Pattern Match and Replacement
scrrun.dll - Scripting Runtime DLL Library
Creating Your Own Classes
IE Web Browser Supporting VBScript
IIS ASP Server Supporting VBScript
WSH (Windows Script Host)
Full Version in PDF/EPUB