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

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.

Related

Immediately executed anonymous function syntax ambiguity [duplicate]

This question already has answers here:
Immediate function using JavaScript ES6 arrow functions
(3 answers)
Why are parentheses required around JavaScript IIFE? [duplicate]
(3 answers)
Closed 3 years ago.
Why is the following a syntax error?
(()=>{console.log(1)}())
()=>{console.log(1)} is clearly(?) an expression that evaluates to a fat arrow function, and () "should" call it?
Presumably because it is ambiguous. What is the ambiguity?
Obviously, I realise the following works:
(()=>{console.log(1)})()

what does this mean? !function() [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 5 years ago.
What does this mean?
!function a(){
}({
x: [function(){alert(2);}]
})
When I reading some codes in a chrome extension, I have found this block of code. I am confused how this should work.
It's just another way of writing an IIFE, using a ! operator instead of parentheses. There are a variety of operators that can be used this way:
! function(text) {
console.log(text);
}('hello');

What (jQuery) means in the end of function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 7 years ago.
I have a function:
var waitingDialog = (function($){
.....
return{
}
})(jQuery);
Also could you explain what $ means in the function? Is it going to work without that?
It means that jQuery (if it exists) will be passed to the function. $ is merely the name that variable will take in the scope of the function.
There is a section about that in official jQuery website : https://learn.jquery.com/plugins/basic-plugin-creation/#protecting-the-alias-and-adding-scope

(function(){})() vs. !function(){}() [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
In jQuery javascript code I see
(function(window, undefined) {
})(window);
And in Twitter
!function(window, undefined) {
}(window);
Can someone tell what the difference between these two approaches is?
Using the ! operator before the function causes it to be treated as an expression, so we can call it:
!function() {}()
http://jasonlau.biz/home/faq/what-is-the-exclamation-mark-used-for-in-code

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

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.

Categories

Resources