How is this working !function(){console.log("hi")}() [duplicate] - javascript

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
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 ()

Look at this answer
tl;dr it defines a function to print out 'hi' and immediately calls it.

Related

What is `(function(document) { ... }(document));` [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 5 years ago.
To be honest, firstly I thought I could easily find answer in Google, but strange, there are very little information about it.
Could anybody explain, what does this code do? What is the advantage (purpose) of using this technique?
(function(document) {
...
}(document));
This is an Immediately-Invoked Function Expression (IIFE) which is basically a function that is declared and called all in one go. It's purpose is to encapsulate logic without muddying up the global namespace (any variables that you declare in that function are only visible in the function). You can find more information here, or just Google IIFE.

Confusion about anonymous function declaration in javascript [duplicate]

This question already has answers here:
javascript function leading bang ! syntax
(7 answers)
Closed 9 years ago.
guys i want to know is there any different between this anonymous :
first :
(function(){
//statements
})();
second :
(function(){
//statements
}());
third :
!function(){
//statmeents
}();
i need a clear explanation about this , thanks all :D
The one and only difference is that the last variation uses fewer bytes.
All three use the language's syntax rules to force the function to be a function expression (which can be immediately invoked) rather than a function declaration (which cannot be invoked, must be named, and is subject to hoisting).

what does !function() means in Javascript? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I can read and write some JavaScript, but today I came across a .js file starting with
!function() {
/*
code here
*/
}.call(window);
Question
what does ! infront of function means?
It returns "not" (!) of the result of a calling the anonymous function with a "this" of window.
Negating the result of .call(window). Not the function.

(function(){})() Is there any documentation of this? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does an anonymous function in JavaScript work?
What does this JavaScript snippet mean?
I use in my scripts:
(function(){
...[code]..
})()
I can't find documentation about this, i have see scripts when this form take args.
(function(){
...[code]..
})(arg1,arg2)
Somebody have a link or a good explanation about this javascript function?
this is just regular javascript.
you instanciate an anonymous function, and then call it with 2 arguments.
The confusing part I think is the on-the-fly aspect of the operation.
You could have done (at higher cost):
var hnd = function() {...};
hnd(arg1,arg2);
It's known as a Self-Executing Anonymous Function.
Here is the first Google result, which gives a solid overview.

What's the difference between these two seemingly similar closure types? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?
I'm curious now that I have seen these two similar examples:
(function ($) {
// code
}(jQuery));
and
(function ($) {
// code
})(jQuery);
Is there any difference and if so what?
There's no difference, they do the same thing. You need the parens around the anonymous function, but whether you put the parens triggering the call within those or outside them doesn't matter. Some feel the former is more "correct" (though I've never heard a strong rationale, just Crockford's assertion that it's more clear, which I contest). The latter (in my experience) is much more common.
having a pair of bracket out side the function definition makes no sense, for now, there's no diff.
these 2 equals function(){}(jQuery);

Categories

Resources