oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Eight Reasons Windows Administrators Should Learn JScript Instead of VBScript

by Bill Stewart

When Windows NT was first released, Microsoft did not provide much in the way of automation except for the Cmd.exe shell. You could always install third-party tools such as Perl (and this is still the case), but the operating system didn't provide much built-in scripting functionality. This changed, though, with the release of the Windows Script Host (WSH) and HTAs (HTML applications). One nice advantage of both WSH scripts and HTAs is that they're both built into the OS on Windows 2000 and later. There are two built-in languages you can use to write WSH scripts or HTAs: VBScript and JScript.

Some administrators have opted for other languages (such as Perl or Python), and Microsoft has itself initiated a new direction with the creation of PowerShell. These are great choices, but they're not built into the operating system, and it's often important to be able to manage the lowest common denominator (e.g., a machine on which you can't install PowerShell). A logon script is a good example: if you write it in Perl, only machines that have a Perl interpreter installed can execute it. PowerShell, while very powerful, has a pretty high barrier to entry (Windows XP service pack 2 or later, and .NET 2.0). By contrast, a WSH script written in VBScript or JScript will run without any prerequisites on Windows 2000 and later (and even on down-level machines that are running Internet Explorer 5.0 or later).

Since most Windows administrators will need to write WSH scripts anyway, it makes sense to use the best language for the job: JScript.

Reason #1: JScript Is More Widely Used

In my experience, there seems to be confusion about what JScript is. In the simplest terms, JScript is Microsoft's implementation of the ECMA-262 specification, which describes a scripting language called ECMAScript. JavaScript and JScript are the two most widely used implementations of the ECMAScript specification. JScript is currently equivalent to JavaScript 1.5, with the exception of a few proprietary extensions that facilitate COM programming. (Note that JScript .NET is a different language, and is beyond the scope of this article.)

The JavaScript/JScript language is used in many different applications on different platforms--most notably, in web browsers. Other vendors have also used it as an automation language (e.g., Adobe). In other words, JScript knowledge can be leveraged in more places. VBScript, by comparison, is proprietary to Microsoft and is only available in Microsoft applications on the Windows platform that use the VBScript interpreter.

Reason #2: JScript Is Alive and Well

As I mentioned above, JScript is based on the ECMA-262 standard. Although the current version of JScript is equivalent to JavaScript 1.5, the upcoming JavaScript 2.0 specification will be backward-compatible with JavaScript 1.5. If JavaScript 2.0 gains wide acceptance, Microsoft will need to update JScript to maintain browser compatibility.

By comparison, VBScript is in a state of "sustained engineering" with Microsoft. This means that Microsoft will only fix major bugs and security holes in the VBScript interpreter; it's not going to be adding any new features or extending the language for other uses. Since VBScript is on life support, it makes more sense to invest in learning a language that has a future.

Reason #3: JScript Is Easy to Learn

One of the reasons VBScript is so widely used in Windows administration is that VBScript has a reputation for being easy to learn. For some reason, this has given JScript a reputation for being hard to learn. This is probably due in part to the fact that JScript's C-like coding style looks different from VBScript. However, once you get past the basics, JScript is easy to learn in its own right. Many of the basic concepts are the same (statements, variables, functions, etc.); they just look different. Once you learn the fundamentals of the language, it's very easy to get your scripting work done using JScript instead of VBScript. Like most Windows scripting authors, I started out using VBScript, but I found that JScript was just as easy to learn as VBScript, even though some of its programming concepts are different.

Reason #4: JScript Is Object-Oriented

JScript is built from the ground up as an object-oriented language. Built-in data types (such as strings and numbers) are themselves objects that have methods. For example, numbers have a toString method that returns the number as a string, and strings have the toUpperCase and toLowerCase methods to return the strings in upper- and lowercase.

It's also easy to create your own objects with minimal code. Consider this example:

var options = { path: "", recurse: false };

This single line of code creates an unnamed object containing two properties and initializes the properties.

You can create a user-defined object in VBScript, but it's a lot more complicated. The language forces you to create a class and an initialization subroutine, like this:

Class OptionsClass
  Public Path
  Public Recurse
  Private Sub Class_Initialize()
    Path = ""
    Recurse = False
  End Sub
End Class

Dim Options
Set Options = New OptionsClass  ' Creates an instance of the class

JScript's object-oriented nature makes this a simple task; VBScript requires quite a bit more code to accomplish the same thing.

JScript also lets you create a named object (which VBScript forces you to do); if you need more than one of these objects, you can simply define a constructor function that defines the object. For example:

function optionsObject(path, recurse) {
  this.path = path;
  this.recurse = recurse;

var options = new optionsObject("c:\\winnt", true);  // Creates an instance of the object

JScript uses prototype-based, rather than class-based, inheritance. Using the prototype property of an object, you can extend an object's behavior by adding a method to its prototype property. For example:

Array.prototype.exists = function(item) {
  for (var n = 0; n < this.length; n++)
    if (item == this[n]) return true;
  return false;

The above code adds the exists method to the Array object. Here's an example of how you'd use the new method:

var myArray = ["a", "b", "c", "d"];  // Defines an array containing four elements
var result = myArray.exists("a");    // Returns true
var result = myArray.exists("e");    // Returns false

This example illustrates how flexible the JScript language is and how it's simple to extend either your own or built-in objects with custom methods.

Reason #5: JScript's Regular Expression Handling Is Better

Regular expressions are a natural part of JScript; they're built into the language's syntax. Regular expression support wasn't added to VBScript until version 5 of the language and, as a result, the syntax is comparatively verbose and awkward. For example, suppose you want to check whether a string looks like an email address using a regular expression. In VBScript, you have to use the New keyword with the Set statement to create a RegExp object, and configure its properties for the search. Here's a code sample:

Address = ""
Set RE = New RegExp
With RE
  .Pattern = "\w+@\w+\.\w+"
  .IgnoreCase = True
End With
Result = RE.Test(Address)  ' Evaluates to True

The equivalent JScript code is more concise:

var address = "";
var RE = /\w+@\w+\.\w+/gi;
var result = RE.test(address);  // Evaluates to true

The second line of code, above, creates a RegExp object. The /expression/ syntax is a convenient way to describe a regular expression pattern when you know the pattern before the code gets executed. Alternatively, you can create RegExp objects using the new operator, like this:

var address = "";
var RE = new RegExp("\\w+@\\w+\\.\\w+", "i");  // \ is JScript's escape character, so '\\' = '\'
var result = RE.test(address);                 // Evaluates to true

This syntax is useful when you want to describe a regular expression pattern at runtime. In addition, JScript RegExp objects have a compile method that provides for increased performance when using them repeatedly, such as in a loop.

Pages: 1, 2

Next Pagearrow