Immediately Executing Functions

By  on  

JavaScript is full of nifty little tricks to accomplish tasks with less code.  One of those tricks is immediately executing functions.  We oftentimes see this pattern for executing anonymous functions to limit variable scope:

(function() {
	console.log('executed!');

	// Do processing here

})();

What many developers don't know is that this code can be shorted by using a ! before the anonymous function:

!function() {
	console.log('executed!');

	// Do processing here	
}()

The function above executes immediately, just as the first snippet did.  One caveat:  the immediately executing function always returns false.  If you desire the result of the anonymous function, you wont want to use this second pattern.

Ben Alman has created an excellent, detailed writeup on the subject and if you want to learn more, be sure to visit his post!

Recent Features

  • By
    CSS vs. JS Animation: Which is Faster?

    How is it possible that JavaScript-based animation has secretly always been as fast — or faster — than CSS transitions? And, how is it possible that Adobe and Google consistently release media-rich mobile sites that rival the performance of native apps? This article serves as a point-by-point...

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

Incredible Demos

  • By
    WebSocket and Socket.IO

    My favorite web technology is quickly becoming the WebSocket API. WebSocket provides a welcomed alternative to the AJAX technologies we've been making use of over the past few years. This new API provides a method to push messages from client to server efficiently...

  • By
    JavaScript Speech Recognition

    Speech recognition software is becoming more and more important; it started (for me) with Siri on iOS, then Amazon's Echo, then my new Apple TV, and so on.  Speech recognition is so useful for not just us tech superstars but for people who either want to work "hands...

Discussion

  1. Bruce Williams

    Because not quite enough people pulled out their hair on encountering the function(){…}() syntax.

  2. The first is not exactly valid. The right call has the call-parentheses inside the container parentheses.

    (function() { ... code ... }())
    

    I think the ! is works with call-parentheses too. So it’s not shorter.

    > !function(){console.log('asd')}
    false
    > !function(){console.log('asd')}()
    asd
    true
    > function(){console.log('asd')}()
    asd
    undefined
    > (function(){console.log('asd')}())
    asd
    undefined
    >
    
  3. Chris

    I think such oddities should be removed from the language.

  4. James Fishwick

    What would ever be the argument for doing this? A Obfuscated Javascript Code Contest?

  5. Why even use such a function? I don’t get it. If you want code to execute immediatly, just write it outside a ‘function’. What’s the point of an anonymous function you can’t call later on for reusability? Or am I missing something?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!