Option Explicit: Why we need this statement while writing VBScript?
-- By default, VBScript will allow use of variables without declaration which leads to lot of mis-spell, type and wrong variable name errors.
-- This will force the VBScript Engine not to accept the variables without declaration before its first use.
-- Normally, Used to avoid typo errors, giving wrong variable names, redefining variables by mistake, etc.
Consider the below script:
- Dim var1
- var1 = 10
- 'This line will not throw any error like "Variable undefined"
- var2 = 20
- 'This line will not throw any error like "Variable redefined"
- Dim var1
- Option Explicit
- Dim var1
- var1 = 10
- var2 = 20 'This line will throw error "Variable undefined"
- Option Explicit
- Dim var1
- var1 = 10
- Dim var1 'This line will throw error "Variable redefined"
The same functionality is available in JavaScript by typing "use strict" like below;
- "use strict";
- var var1 = 10;
- alert(var1);
Comments
Post a Comment