I read this on the javascript garden site. Can someone explain how it works?
!function(){console.log("hi")}()
I read this on the javascript garden site. Can someone explain how it works?
!function(){console.log("hi")}()
The 'executing' parens at the end can't be done legally after a function expression. A typical (more sensical, IMO) way of writing this is with more parentheses:
(function(){console.log('hi')})()
By prepending the !
before the function expression, the JS interpreter reads the function and then runs it. This is because of the precedence of the !
operator vs. calling a function with the final ()
tl;dr it defines a function to print out 'hi' and immediately calls it.