Code Renaissance is about building great teams and great software. By exploring best practices, team interactions, design, testing and related skills Code Renaissance strives to help you create the team and codebase that you've always wanted.

The Javascript void operator explained

I haven't seen a clear explanation of why we have the void operator in javascript so I decided to do a bit of research myself.

Picture (of Javascript Graffiti Art?) by S. Mason

The javascript void operator was introduced in the Javascript 1.1 specification. The explanation of void states:

  • The void operator takes an expression of any type as its operand, and returns undefined.

This is pretty straight forward though it's not entirely clear why that would be useful. One thing this clarifies is that void is an operator (not a function). Because of this void(0) is technically incorrect though in practice implementations allow it to be used this way it should be used without parentheses e.g. void 0. Also because it is an operator you can't assign it to a variable. This means that "var a = void" will throw an error.

Another thing that's important is that when it the specification mentions undefined it is not talking about the undefined keyword which is a global variable that wasn't introduced until javascript 1.3; instead it's talking about the undefined type which is a primitive type like string and number in javascript.

I also found another mention of void in the document:

  •  When a JavaScript expression is evaluated (executed), the result denotes one of
    three things:
    • a variable (in C, this would be called an lvalue)
    • a value
    • undefined (the expression is said to be void)

From this you can tell that undefined is strongly related to concept of void in C. In some early coding examples elsewhere I found code that said "if(a === void 0)", so it looks like early on (in practice if not by intention) void was a common way to get the undefined value for comparison. For instance the following will display true: javascript:alert(undefined === void 0).

This can still be useful. As of the currently implemented specification the undefined property doesn't have to equal undefined, so javascript:undefined=4;alert(undefined === void 0) returns false (though in IE9 and Firefox 4, per the ECMA 5 spec, undefined is no longer writable, so this returns true). Because of this, if you are writing code that will be executed in someone else's web page (an ad perhaps) you may want to use void 0 in place of undefined. As more browsers support the ECMA script 5 specification this will be less of an issue.

Some sources I've read suggest the sole reason that void was put in specifically for anchor tags (a hack that is so often debated), but I have found no evidence of that.

I hope this information helps though, outside of anchor tags, I'm still unclear on it's real usefulness. If you have any coding examples that show where void is helpful please share.

1 - What do you think?:

JavaScript Training said...

What a fantastic article about JavaScript void. A unique explanation