What does the exclamation mark do before the function in JavaScript?



The ! symbol shows that it is an immediately-invoked function expression.

The exclamation mark won’t invoke the function alone; you can put () at the end −

!function foo() {}()

() has higher precedence than ! and instantly calls the function.

You can also mention it like the following −

(function(){})();

The ! allows the expression to return true. This is because by default all immediately-invoked function expression return undefined, so, we’re left with ! undefined, which is true.


Advertisements