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

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -