Bye Bye VBScript

by Mauricio Rojas, on Aug 5, 2019 1:48:42 PM

Internet Explorer is still a very used browser and VBScript on some of them is the scripting engine of choice.

However it seems like Microsoft decided that it was time to give the good old VBScript a rest.

In this post I will give you a basic comparison of VBScript and Javascript so you can use it as a guide when upgrading your web pages.

Case Sensitivity

VBScript is case-insensitive, and I think this is one of the things you will miss more and probably one of the hardest things to manually fix. Why? Well because in VBScript you could write myVar, MyVar, MyvaR and MYVAR (yes you can even yell in VBScript) and that is no problem at all. However in Javascript each of those is a completely different variable. So making sure that all variable and function names and consistent in all your codebase is probably going to require some patience.

Subroutines and Functions

In VBScript you have subroutines (which do not return a value) and functions (that return a value). In Javascript you only have functions. But this is not going to be much of a problem

Comments

In VBScript comments start with an apostrophe ' or with the REM keyword. Everything after the apostrophe or REM till the end of the line is a comment. There are no multi line comments. In Javascript you start your single-line comments with // and you can have multi line comments, starting with /* and ending with */

Data Types

VBScript has only one data type called Variant and you are gonna miss it :( a variant can have several subtypes:

  • Empty
  • Null
  • Boolean
  • Byte
  • Integer
  • Currency
  • Long
  • Single
  • Double
  • Date (Time)
  • String
  • Object
  • Error

Types are a little different in Javascript. In Javascript unassigned values belong to the undefined data type which is more or less the VBScript Empty object. So when you assigned Nothing in VBScript you will now assign undefined.

Javascript has 7 basic data types

  • number for numbers of any kind: integer or floating point
  • string for strings
  • boolean true/false
  • null for unknown values
  • undefined for unassigned values
  • object for complex data structures
  • symbol for unique identifiers

You can use the typeof operator to see the type stored in a variable.

Handling Exceptions

Another thing you will miss from VBScript. Error handling. In VBScript we had the Err object. You could call the Err.Clear to reset the exception indicator. You could just use the On Error Resume Next, meaning that if an error happened your code just continue running and you could eventually check the Err.Number or Err.Description to look for errors.

Well that is gone. Javascript uses the try...catch pattern which works well but it not as syntactically nice as it was on VBScript. On the catch block you get an Exception object and it will have a message property.

Arrays

Well arrays are almost the same, I think that main difference is that you used indexes line (index) and now the syntax is [index] but in general they are pretty similar.

Builtin Functions

Date/Time Functions

  • CDate
  • Date
  • DateAdd
  • DateDiff
  • DatePart
  • DateSerial
  • DateValue
  • Day
  • FormatDateTime
  • Hour
  • IsDate
  • Minute
  • Month
  • MonthName
  • Now
  • Second
  • Time
  • Timer
  • TimeSerial
  • WeekDay
  • WeekdayName
  • Year

In Javascript these functions could be mapped to Javascript libraries like moment.js

Conversion Functions

  • Asc closest is maybe String.fromCharCode
  • CBool just use the expression anything greater than 0 is true
  • CByte create a helper in js
  • CCur there is no currency time in js just use number
  • CDate use js date or moment.js functions
  • CDbl use parseFloat
  • Chr String.fromCharCode
  • CInt use parseInt
  • CLng use parseInt
  • CSng use parseInt
  • CStr in most cases use .toString()
  • Hex yourNumber.toString(16)
  • Oct yourNumber.toString(8);

Math Functions

  • Abs
  • Atn
  • Cos
  • Exp
  • Hex
  • Int
  • Fix
  • Log
  • Oct
  • Rnd
  • Sgn
  • Sin
  • Sqr
  • Tan

Almost all have an equivalent on the Math object

Format Functions

  • FormatCurrency you can use the Internationalization API for example: var formatter = new Intl.NumberFormat('en-US', {
    style: 'currency',
    currency: 'USD',
    }); formatter.format(2500); /* $2, 500.00 */
  • FormatDateTime use moment.js
  • FormatNumber use Intl.NumberFormat
  • FormatPercent use Intl.NumberFormat

Array Functions

  • Array you can just create new array like ["one", "two", "tree"]
  • Filter use myarray.filter
  • IsArray use Array.isArray
  • Join use myarray.join
  • LBound all arrays start at 0
  • Split use mystring.split
  • UBound use array.length

String Functions

  • InStr use mystring.indexOf(searchStr)
  • InStrRev
  • LCase use mystring.lower()
  • Left use substring
  • Len mystring.length
  • LTrim mystringthis.replace(new RegExp("^[\s]+"), "")
  • RTrim mystring.replace(new RegExp("[\s]+$"), "")
  • Trim mystring.trim()
  • Mid use substring
  • Replace use can use mystr.replace, regular expressions or create a custom function to match vbscript functionality
  • Right use substring
  • Space " ".repeat(n)
  • StrComp mystr.localeCompare
  • String use toString
  • StrReverse use split, reverse and join to create an equivalent
  • UCase mystring.upper()

Other Functions

  • CreateObject well this probably deserves another post :P
  • Eval you can use eval like: eval('x = 42');
  • IsEmpty == undefined
  • IsNull == null
  • IsNumeric something like: function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
    }
  • IsObject typeof or instanceof
  • RGB just create an equivalent helper
  • Round Math.Round
  • ScriptEngine no equivalent
  • ScriptEngineBuildVersion no equivalent
  • ScriptEngineMajorVersion no equivalent
  • ScriptEngineMinorVersion no equivalent
  • TypeName use something like typeof
  • VarType use something like typeof

Well this is just a shallow comparison between VBScript and Javascript. For those hitchhikers to end of the galaxy on this voyage, remember:

Don't Panic
Time is an illusion. Lunchtime doubly so

 

 

Topics:application migrationJavaScriptievbscript

Comments

Subscribe to Mobilize.Net Blog

More...

More...
FREE CODE ASSESSMENT TOOL