The second form function () {}
is a statement. The !
operator converts this into an expression. You will also find cases where people use -
or +
before the function
keyword.
When you have an expression evaluating to a function, you can call that function by using the ()
operator.
Another (perhaps easier to understand) way to achieve the save same effect is with another set of parenthesis:
( function(x) { body; } )(arg);
By placing the function inside the parenthesis, you again convert it to an expression, which evaluates to a function. This function is called with arg
as an argument.
As an arrow function:
( (x) => { body; } )(arg);