Over a million developers have joined DZone.

The void operator in JavaScript

· Web Dev Zone

Make the transition to Node.js if you are a Java, PHP, Rails or .NET developer with these resources to help jumpstart your Node.js knowledge plus pick up some development tips.  Brought to you in partnership with IBM.

Syntax:

    void expr

evaluates expr and returns undefined. Its (rare) use is for contexts where an expression is demanded, but the result has to be undefined. Example: javascript URLs in browsers display the result of an expression, unless it is undefined. Compare:

  • javascript:3+4 displays 7 as the current document.
  • javascript:void(3+4) does not change the current document. void “hides” the result of the expression 3+4.

Similarly:

  • javascript:window.open("http://www.whitehouse.gov/") replaces the content of the current page.
  • javascript:void window.open("http://www.whitehouse.gov/") does not change the content of the current page.

Comments:

  • The operator binds very closely, thus you should put parentheses around its argument if it contains more than a single token. For example, void 3+4 binds as (void 3)+4.
  • void 0 and void(0) are the same as undefined, with one exception: undefined can be shadowed.
        function a() { var undefined = "hello"; return undefined; }
    a()
    ’hello’
    In contrast, void cannot be redefined:
       function b() { var void = function() {}; return void(0); }
    This results in a syntax error complaining about a “missing variable name” (after var).

From http://www.2ality.com/2011/05/void-operator.html

Learn why developers are gravitating towards Node and its ability to retain and leverage the skills of JavaScript developers and the ability to deliver projects faster than other languages can.  Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}