7

when looking at the minified Sizzle code, I noticed that it begins like this:

!function(a){//... }(window) 

Why is there an exclamation point at the beginning?

I thought that ! was the not operator.

Thank you.

Edit:

Full Code.

7
  • 2
    it IS the NOT operator. MInd to show the whole code? I guess this isn't a function declaration but rather an IIFE... Commented Feb 5, 2014 at 3:51
  • 1
    Are you sure that it's not something like !function(){...return bool;}() Commented Feb 5, 2014 at 3:51
  • @p.s.w.g: it shouldn't return boolean Commented Feb 5, 2014 at 3:53
  • Guess that's meant to be pseudo-code, but trueor falsewould do better, yes. Commented Feb 5, 2014 at 3:53
  • For context, the code is on github. If you understand what minification does, then I wouldn't question seemingly obscure syntax. Commented Feb 5, 2014 at 3:54

2 Answers 2

6
!function(a){/* ... */}(); 

Using an unary operator to invoke an IIFE is common practice. That's a common shorthand for:

(function(a){/* ... */}()); 

or:

(function(a){/* ... */})(); 

You can also substitute the not unary operator with any other unary operator:

-function(a){ /* ... */ }(); +function(a){ /* ... */ }(); /* ... etc. */ 
Sign up to request clarification or add additional context in comments.

14 Comments

You mean !function() {}() doing !function(){} won't call it
@remyabel Closures usually don't have one, and almost never when used as an IIFE. Actually, (function Name () { ; }) ()isn't valid, the "normal" funciton statement (function name () { ; }) most people use is just a shortcut for var name = function () { ; }.
Please note that the ! does not invoke the function. It's purpose is to make the parser treat the function definition like an expression, not a statement.
@JohannesH.: In older IE versions it does (it's a bug). The specification dictates that the name of a function expression is only visible within the function itself. It's great for debugging and recursive functions.
+1 But be wary of using the + and - operators for this since they're overloaded as binary operators as well. So if you omit a semicolon on the previous line, it can give an undesired result. The unary operators that are not overloaded are best, like !, void, ~, etc.
|
4

gives a good explaination for function invocation https://github.com/airbnb/javascript/issues/44#issuecomment-13063933

!function () {}(); 

is equivalent to

(function(){})(); 

except the author is saving 1 byte of code.

In many cases, it's about saving bytes.

!function aaa(){}() !function bbb(){}(); 

is the same as this:

!function aaa(){}() ;(function bbb(){})(); 

notice the ";" in that last bit. That is defensive, as it protects your code a bit from runaway js that might preceed it.

funny, I asked this same question some time ago:

Came across a convention I've never seen. What does it do? !function

great reference on it: What does the exclamation mark do before the function?

1 Comment

Just for reference: in other programming languages, similar things are common. In bash scripts for example, the "do nothing command" (:) is often used to evaluate variable expansions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.