A Little More on Nothing

VBScript has Null, Empty and Nothing. What about JScript? Unfortunately, JScript is a little screwed up here.

Like VBScript, in JScript an uninitialized variable is a VT_EMPTY variant. To check to see if a variable is undefined, you can say

typeof(x) == "undefined"

There is a built-in Empty constant in VBScript, but not in JScript.

If you say

var x = null;
print(typeof(x));

then you get object, so you might expect that null is the JScript equivalent of Nothing — an object reference that refers to no object. From a logical perspective, you’d be right. Unfortunately, for reasons which are lost to the mists of time, JScript implements a null variable internally the same way that VBScript implements a Null variable — VT_NULL, the “data is missing” value.

This poor decision leads to two problems. First, JScript does not interoperate very well with COM objects which have methods that can legally take a Nothing but not a Null. (I believe that Peter Torr has some addon code that adds this capability to JScript.) Second, JScript processes Null variants passed into it as though they were object references, not database nulls. Thus JScript does not implement any of the rules about null propagation that VBScript implements.

We corrected these problems somewhat in JScript .NET. JScript .NET uses a null object reference to internally represent null, and can manipulate DBNull objects, so the interop problems go away. However, JScript .NET still does not implement the null propagation semantics for mathematical operations on database nulls.


Commentary from 2019:

Reader response to this article was pretty positive, with several readers noting that getting the insider perspective on the implementation decision of programming language minutia was valuable to them. As I’ve said many times before, getting that sort of information available to the public was my whole reason for starting this blog, so I’m glad to have succeeded.

One reader requested a JS linter; recall that this was still 2003 and we did not have the sort of JavaScript infrastructure available that we did today. Peter Torr noted that you could use the JScript.NET compiler as a linter, as its error detection was pretty good. And I noted that I’d be posting more about language and browser design decisions that make linting harder.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s