2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

What does the exclamation mark do before the function?

i"m a newbie to javascript... please let me know what does this exclamation is for?

!function () {}();

userimage

You can declare a function the following way

function one() {}

Note that there's no semicolon in this function declaration; you need a separate invocation of one() to run the function.

On the other hand, !function one() {} is an expression, but it still doesn't invoke the function, but we can use !function one() {}() to do that, as () has higher precedence than !. The original example function doesn't need a self-reference so that the name then can be dropped.

So what the author is doing is saving a byte per function expression; a more readable way of writing it would be this:

(function(){})();


Answer is