VBScript Constants

VBScript Constants

  1. Constant is name given to a memory location which is used to hold the value and whose value cannot be modified during program/script execution.
  2. VBScript execution will throw an error if we attempt to modify the value of constant during script execution
  3. VBScript constant declaration is same like variables declaration.
  4. VBScript constant is declared using ‘const’ keyword
  5. VBScript constant can be public or private. Access modifier are optional in VBScript
  6. VBScript constant declared with public are available for all the scripts and procedures.
  7. VBScript constant declared with private will be available only within the procedure or class.
  8. Possible values that can be assigned to constant : Number, String or Date.

Declaring VBScript Constants

Syntax

[Public | Private] Const Name_Of_Constant = Value

Example 1 : Declaring Constant

We have declared a constant variable whose value cannot be changed during the script execution

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Const constant_var = 10
document.write("Value of Const Variable : " & constant_var) 
</script>
</body>
</html>

Example 2: Assigning Date and String

In this example we have assigned string and date value to a constant.

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Const str = "www.c4learn.com"
Const dob = #22/10/2010#
document.write("Print Constant String : " & str) 
document.write("Print Constant Date   : " & dob) 
</script>
</body>
</html>

Example 3 : Reassigning Value

If we try to re assign any value to constant variable then script will throw execution error

<!DOCTYPE html>
<html>
<body>
<script language="vbscript" type="text/vbscript">
Const str = "www.c4learn.com"
Const dob = #22/10/2010#
document.write("Print Constant String : " & str) 
document.write("Print Constant Date   : " & dob) 
Const dob = #22/10/2011#
</script>
</body>
</html>