VBScript/JScript comparison

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



I have read with interest the posts in this newsgroup related to the
differences between VBScript and JScript. Torgeir Bakken mentioned that
in his environment, they chose VBScript over JScript for administrative
scripting tasks:

There were several reasons for this, e.g. there are much more
examples and finished code for admin scripting in VBScript, and also
JScript has some issues with ByRef and COM interfaces that VBScript
does not have.

These are interesting points, and I'd like to post my own list of that highlights some of the important differences between the two languages. I've only studied JScript for a couple of weeks now, so there may be some other important differences. These are just the differences I've noticed so far. Also, I am focusing on scripting in the WSH environment, not in browsers (hence this newsgroup).


Of course, comments are welcome.

* Forced variable declaration. VBScript has the Option Explicit statement to require variable declaration. JScript doesn't have a way to do the same thing. In JScript, you can't use a variable before it's defined, but that's not quite the same thing.

* Constants. VBScript has the Const statement. JScript has no equivalent.

* Case-sensitivity. VBScript isn't case-sensitive; JScript is.

* Passing parameters to functions by reference or by value. In VBScript, you can use the ByVal or ByRef keywords in the Sub or Function declaration to specify the parameter-passing method. With JScript, the parameter-passing convention is determined by the parameter's data type: Primitive types (boolean, numeric) are always passed by value, and reference types (objects, arrays, functions) are always passed by reference.

* Arrays. VBScript arrays (also called safe arrays) are zero-based and can be multi-dimensional. The UBound() function determines the array's upper bound. A JScript array, is a special object type that has a length property and can also be sparse (non-contiguous). Arrays indices in JScript need not be numeric and so can function like associative arrays. A VBScript array returned from a COM object (such as a WMI method) can be converted to a JScript array using the toArray() method. A JScript array can't be used as input to a COM object method that's expecting a VBScript array.

* COM collection enumeration. In VBScript, you can use For Each ... Next directly. To enumerate collections in JScript, you have to use an Enumerator object and access the current member by calling the Enumerator object's item() method.

* Objects. JScript allows you to extend intrinsic and user-defined objects by adding methods to the object's prototype. JScript's entire design is different in this regard. You can create an "object" in VBScript by defining a Class, but there's no built-in objects in VBScript except for the Err and RegExp objects, which can't be extended with additional methods. (Objects included with the scripting runtime, such as Scripting.Dictionary, WScript.Shell, etc. don't count because they're not part of the VBScript language.)

* Intrinsic objects. VBScript only has two built-in objects: Err and RegExp. JScript has a whole group of built-in objects. Even its primitive types are based on intrinsic objects that can be extended.

* Error handling. JScript uses try ... catch blocks, where VBScript uses On Error Resume Next.

--
Bill Stewart
.



Relevant Pages

  • Re: VBScript/JScript comparison
    ... > differences between VBScript and JScript. ... > reference types (objects, arrays, functions) are always passed by ... > Enumerator object's itemmethod. ...
    (microsoft.public.scripting.wsh)
  • Re: Object Rexx to become Open Source
    ... Granted WSH is not a language. ... >> up quickly, then the knock against JScript would be that, because it is ... never said that JScript was bad or even not as good a language as VBScript; ... But our official standard scripting language for such things as logon ...
    (microsoft.public.scripting.wsh)
  • Re: VBScript/JScript comparison
    ... > differences between VBScript and JScript. ... > Enumerator object's itemmethod. ... > primitive types are based on intrinsic objects that can be extended. ...
    (microsoft.public.scripting.wsh)
  • Re: Object Rexx to become Open Source
    ... We were talking "languages", WSH is just an interface to the o/s. ... > as provided by some other scripting languages, I have found it a significant ... source" on any of their pages and you'll see it's JScript all the way. ... also can't imagine using VBScript for XML?? ...
    (microsoft.public.scripting.wsh)
  • Re: Object Rexx to become Open Source
    ... If standards don't matter too much ... VBScript, but both Visual Basic and VBA are dead! ... One other thing you can do in JScript is comment out huge blocks of code ... server, you just put the module with your code and off you go. ...
    (microsoft.public.scripting.wsh)