Clinick's Clinic on Scripting #1: VBScript or JScript?
May 4, 1998
The two questions a program manager for the Microsoft Script Group hears most frequently:
- "Should I use VBScript or JScript®?"
- "Is Microsoft dropping VBScript for JScript?"
For the first in an occasional series of feature articles in MSDN Online on scripting, I thought I'd tackle this thorny problem and explain our philosophy behind each language and how that affects your choice, since it's up to you which language you're going to use.
For many, the choice of programming language takes on an almost religious zeal. If you program in C, there's no way I'm going to convince you that Basic is the language for you. Likewise, if you're a Basic programmer, you're certainly not going to want to learn an unfamiliar syntax and a different way of thinking. The Microsoft Script Group is committed to providing you with a choice of languages. This level of choice begins with VBScript and JScript from Microsoft—but it doesn't stop there, as other languages (Perl, Python, and so on) are all equally useful in the Microsoft scripting solutions (Internet Explorer, Internet Information Server, Windows Script Host).
Factors that will affect your decision about which language to use:
- Target platform
- Language features
- Personal preference/existing knowledge
The platform you are developing for is going to have a big impact on what language you are going to choose. The decision is split between the client and the server.
On the server, the decision is not influenced by target platform, since you control the server. This assumes that you are using a Microsoft server product. If you are developing for the server, the ability to script external COM objects is probably going to be more important than language features, because that's where most of the server functionality will be implemented. For example, the ability to cope with errors raised by COM objects quite probably will outweigh the ability to use extended language features.
If you are planning to script HTML on the Internet and would like the "broad reach" of supporting both Netscape and Microsoft browsers, then your choice of languages is limited to JScript. Netscape does not support VBScript, so if you want to ensure that your page is viewable with modern browsers, you need to use JScript. Using JScript, however, does not guarantee that your script will work on all browsers, since there are many differences in HTML object-model conformance among the leading browsers. If you stick to features in HTML 3.2 (see the World Wide Web Consortium, or W3C, specification and ECMAScript (see the ECMA Standard, you should be okay.
If you are planning to develop to the Dynamic HTML (DHTML) platform, the decision becomes a little more complex. The W3C Document Object Model and HTML 4.0 define the state of the art for Web development, but the available version 4.0 browsers comply to these standards at different levels. If you plan to use every feature in the DHTML book, you're probably going to use Internet Explorer 4.0. This makes the decision a little easier, since Internet Explorer 4.0 supports both VBScript and JScript—except on the Macintosh, where only JScript is supported at the moment. So if you are planning to develop for Internet Explorer, and don't anticipate Macintosh users coming to your site—on an intranet, for example—then you can use either language.
VBScript is designed as a subset of the full Visual Basic® programming system (see Visual Basic for Applications Features Not In VBScript. The guiding principles of VBScript are:
- Provide the Basic developer with a path to Web development on the client and server.
- Bring an easy-to-use-and-understand scripting language to the Web.
- Expand the Visual Basic family of languages to platforms not covered by Visual Basic or Visual Basic for Applications.
Most readers will think of using VBScript in three primary kinds of applications: Internet Explorer, Internet Information Server, and Windows Script Host. I will cover those three specifically, but all the pros and cons are applicable to other applications as well.
- Easy to understand and master. VBScript's biggest asset is that it's Basic, a language designed to be learned quickly. Visual Basic has brought Basic users some of the advantages of more complex languages, while not losing sight of the fact that the language should be easy to understand.
- It's Visual Basic. Visual Basic has been phenomenally successful since its introduction in 1991; there are now over 3 million Visual Basic developers. All the skills they've learned with it instantly apply to VBScript. Also, an enormous community exists to provide training, books, and magazines for learning Visual Basic.
- Flexible. VBScript can be used in a wide variety of applications, and Microsoft is committed to ensuring that wherever script is part of an application, VBScript will be included.
- Error handling. VBScript has a subset of the error handling provided by Visual Basic. This includes the error object and on error resume next. Error handling is very important when developing server-side code, since most of the functionality will require access to external COM objects, which could throw errors.
- Formatting. VBScript has the ability to format dates, numbers, and currency built into the language.
- Easier COM Interop. Many COM objects return information in the form of a collection. VBScript has built-in support for iterating through collections
- Standard Event-binding Syntax. Visual Basic developers will immediately recognize the object_event (sub button1_onclick) naming convention for event handlers. VBScript works in exactly the same way, so in any application that supports event binding (such as Internet Explorer, Outlook, and Windows Script Host), you can use this syntax for your event hook-up code. This also means that you can cut and paste existing Visual Basic code into VBScript (assuming you are using features that are in VBScript).
JScript was designed as a general-purpose scripting language that would appeal to the many programmers who use C, C++, and Java. This means that it "borrows" features from these languages where appropriate, but is a language in its own right and includes many features not found in C or Java. It's important to note that JScript is not just confined to use in the browser; you can use JScript in most applications in which you can use VBScript.
- Broad reach. JScript is pretty much guaranteed to run in any browser, anywhere. If your page really must work in any browser, this is the language for you.
- All those books in the bookstore. How many books can there be? If you want to learn JScript, there are myriad books to help you.
- Similarity to C and Java. If you are a C or Java programmer, the JScript syntax is going to be familiar to you.
- Dynamic: JScript was designed as a completely dynamic language; that is, you can effectively redefine your program on the fly. While this has a number of potential disadvantages, it does give you the ultimate flexibility in your scripts. This is particularly useful in DHTML programming, since DHTML allows you to dynamically manipulate the object model. If you really want to drive DHTML, you might want to consider using JScript instead of VBScript.
- Object oriented: JScript certainly isn't a traditional class-based, object-oriented language, but it does provide an effective alternative based on prototypes. This allows you to reap the benefits of object orientation without the statically defined nature of classes.
- Regular expressions: A main reason why Perl has such a huge following. Regular expressions add the ability to search for expressions in strings. This is exceptionally useful on the server and, increasingly, on the client.
- Eval: Provides the ability to immediately evaluate code at runtime. This allows you to dynamically redefine logic dependent at run time. This is especially useful when used in conjunction with Remote Scripting, which works with VBScript as well.
In the Microsoft Script Group, we are working hard to ensure that each language is functionally equivalent. This doesn't mean that VBScript will suddenly become less Basic-like, or that JScript will become more like Basic; only that we strive to add features that fit into the spirit of the language. If we succeed in this, your language choice should be merely a matter of personal choice and the platform you target. Most importantly, Microsoft is fully committed to supporting any script language through the ActiveX Scripting architecture. In addition to providing this scripting platform, Microsoft is fully committed to evolving implementations of both JScript and VBScript to a level where each language is functionally equivalent. So don't feel that you need to commit to one just to ensure that you are investing in a language that has a future.
Andrew Clinick is a Program Manager for the Microsoft Script Group.