VBScript/JScript comparison
- From: Bill Stewart <llib.trawets@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 03 May 2005 16:15:00 -0600
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 .
- Follow-Ups:
- Re: VBScript/JScript comparison
- From: zwetan
- Re: VBScript/JScript comparison
- From: Al Dunbar [MS-MVP]
- Re: VBScript/JScript comparison
- Prev by Date: Re: Yet another Newbie asking for assistance
- Next by Date: Re: Help Decoding HEX Registry Entry
- Previous by thread: Help Decoding HEX Registry Entry
- Next by thread: Re: VBScript/JScript comparison
- Index(es):
Relevant Pages
|