Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript !function(){} [duplicate]

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.

like image 243
Progo Avatar asked Feb 05 '14 03:02

Progo


People also ask

How to check if an array has duplicate values in JavaScript?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.

How to check duplicate value in array?

function checkIfArrayIsUnique(myArray) { for (var i = 0; i < myArray. length; i++) { for (var j = 0; j < myArray. length; j++) { if (i != j) { if (myArray[i] == myArray[j]) { return true; // means there are duplicate values } } } } return false; // means there are no duplicate values. }


1 Answers

!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. */
like image 67
Alex Avatar answered Sep 19 '22 06:09

Alex