JavaScript for C# programmers: void

A very short, sweet post in the series for C# developers learning JavaScript.

In this episode, we look into the void.

In C#, void is a type that has no values. We talk of a method returning void, when we actually mean the method doesn’t return anything — it’s a procedure and not a function. Well in JavaScript, all functions and methods return something and if you don’t explicitly use the return keyword, the function returns undefined. If you run the following code in Firebug, you’ll get "return type is undefined" displayed.

var testFunc = function(a) {
    console.log(typeof a);
}
console.log("return type is " + typeof testFunc(42));

Cool, eh? So,, what does this have to do with void? Well, void is an operator taking a single parameter, and that returns undefined. The parameter can be any type, including undefined.

console.log("return type is " + typeof void("whaaa?"));

So, why exactly is this useful? Well, it isn’t really. People use void(0) as a kind of placeholder for anchor links when they don’t want to use the anchor link, but to instead attach an onclick handler for the <a> element. The browser won't reload the page if the link evaluates to false; it's as if the link were dead. Stuff that, use "#" instead, or use a real link in case the end-user has JavaScript turned off. (This philosophy is usually known as unobtrusive JavaScript: the site ‘works’ if JavaScript is turned off, but gives a better experience if it is turned on. Using void(0) as a link assumes JavaScript is turned on.)

As Douglas Crockford says, succinctly: Avoid void.

Album cover for Platinum & Gold Collection: The Alan Parsons Project Now playing:
The Alan Parsons Project - Eye in the Sky
(from Platinum & Gold Collection: The Alan Parsons Project)



Posts on similar topics...

1 Response

  • Tue 17 Feb 2009
  • 8:26 AM
  •  avatar #1

Dew Drop - February 17, 2009 | Alvin Ashcraft's Morning Dew said...

Pingback from Dew Drop - February 17, 2009 | Alvin Ashcraft's Morning Dew

Leave a Response

Some MarkDown is allowed, but HTML is not. (Click to learn more.)

  • Emphasize with italics: surround word with underscores _emphasis_
  • Emphasize strongly: surround word with double-asterisks **strong**
  • Link: surround text with square brackets, url with parentheses [text](url)
  • Inline code: surround text with backticks `IEnumerable`
  • Unordered list: start each line with an asterisk, space * an item
  • Ordered list: start each line with a digit, period, space 1. an item
  • Insert code block: start each line with four spaces
  • Insert blockquote: start each line with right-angle-bracket, space > Now is the time...

Extras

Search

About Me

I'm Julian M Bucknall, the M because it's my middle initial and because I and the other Julian Bucknall (the movie guy) would like to differentiate ourselves.

I'm a programmer by trade, an actor by ambition, and an algorithms guy by osmosis. I write articles for PCPlus in my spare time, not that there's much of that.

Julian M Bucknall Apart from that, an ex-pat Brit, atheist, microbrew enthusiast, Pet Shop Boys fanboy, slide rule and HP calculator collector, amateur photographer, Altoids muncher.

DevExpress

I'm Chief Technology Officer at Developer Express, a software company that writes some great controls and tools for .NET and Delphi. I'm responsible for the technology oversight and vision of the company.

Validation

Validate markup as HTML5 (beta)     Validate CSS

Bottom swirl

Archives

May 2012 (4)
SMTWTFS
« Apr  
12345
6789101112
13141516171819
20212223242526
2728293031

Like this Archive Calendar widget? Download it here.

Social networking

The OUT Campaign

The OUT Campaign

My Tweets

  • Honest Movie Trailer of Phantom Menace http://t.co/sif8y4Ns and then Battleship, er, Transformers http://t.co/sif8y4Ns
  • Damn, Donna Summer and Chuck Brown both gone in the last 24 hours. Different types of music, sure, but enjoyed them both. :(
  • Just saw a company page showing a list of tweets with "Join the conversation" linked to their Twitter a/c. The tweets are 6 months old #fail
Bottom swirl