I have a simple question.
I've found this code, and i don't know this statement
!function ($) {
// (...)
}(window.jQuery);
why put ! before a function?
i've found this on bootstrap.js file, and i really want to know.
Thanks!
I have a simple question.
I've found this code, and i don't know this statement
!function ($) {
// (...)
}(window.jQuery);
why put ! before a function?
i've found this on bootstrap.js file, and i really want to know.
Thanks!
It is a duplicate as nnnnnn mentioned. What the code is doing is executing the anonymous function while passing window.jQuery as a parameter, which will be referenced as $ inside the function. This allows the use of $ to reference jQuery without conflicting with any other library that might use the dollar sign.
This is a more readable version of the code:
(function($){
// here, $ references jQuery and any variable or function
// declared here cannot be overridden outside of this function
})(window.jQuery)
The ! will always parse the statement as being true if the statement is not able to be parsed.
You can see this by using,
javascript:alert(!function(){}())
Where the resulting response is true