1

Possible Duplicate:
What does the exclamation mark do before the function?

I came across this today and have never seen before:

!function($) {
//contents removed

}( window.jQuery );

I am specifically wondering what the exclamation point does. Is there any documentation on it? Internet searches haven't yielded good results.

Thanks!

Community
  • 1
  • 1
Itumac
  • 647
  • 2
  • 7
  • 16
  • 1
    The `!` is negating the result of the function call. – Chris Farmer Jan 02 '13 at 21:53
  • 2
    It is the same as `!expr` where expr is `function() { .. }()` or an IIFE - Immediately Invoked Function Expression. (The `!` is useless, but may exist to prevent the "accidental" omission of a semi-colon under ASI.) –  Jan 02 '13 at 21:53
  • 3
    http://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function May be helpful –  Jan 02 '13 at 21:53

1 Answers1

3

An exclamation mark before a function statement creates a function expression. If you want to create a function which invokes itself, it must be an expression not a declaration.

One could achieve the same result by using a + character for instance, or putting the whole expression into parenthesis.

+function( $ ) {}( window.jQUery );

or

(function( $ ) {}( window.jQuery ));
jAndy
  • 231,737
  • 57
  • 305
  • 359