javascript - What does the exclamation mark do before the function? -
!function () {}();
javascript syntax 101. here function declaration:
function foo() {}
note there's no semicolon: function declaration. need invocation, foo()
, run function.
now, when add seemingly innocuous exclamation mark: !function foo() {}
turns expression. function expression.
the !
alone doesn't invoke function, of course, can put ()
@ end: !function foo() {}()
has higher precedence !
, instantly calls function.
so author doing saving byte per function expression; more readable way of writing this:
(function(){})();
lastly, !
makes expression return true. because default iife return undefined
, leaves !undefined
true
. not particularly useful.
Comments
Post a Comment