This question already has a nice answer from Amit joki
I had read a comment below his answer there the questioner was said that he need more explanation that's why i am posting this answer .
First Case:
var myVar = !function(){ return false; }( );
alert(myVar);
Here myVar is an anonymous function and through the below code it calls itself.
var myVar = !function(){ return false; }( );
That is it, is a self executing anonymous function!
.
a !function()
Using the ! operator before the function causes it to
be treated as an expression, so we can call it:
So when we try to alert(myvar);
then it will alert the return value of the myVar
function . Already the myVar
function return a false
and the !
symbol in our code will convert it to true
So it will return true
Think our code is something like below
var myVar = function(){ return 10; }( );
it will also alert true
. Because in javascript only false, NaN, null, undefined, 0 , ""
are false and other all are truth
.
and when the above code changes to
var myVar = !function(){ return 10; }( );
then it will alert false
because the statement return 10
is true in javascript and it will converted to false
by !
operator
Then in second case :
var myVar = !function(){return true};
alert(myVarz);
Here is no self executing anonymous function. only an anonymous function definition is here. When we try alert(myVar)
myVar
is now a function, which is truthy
because in javascript only false, NaN, null, undefined, 0 , ""
are false
and other all are truthy
.!!!
And here also !
comes in frond of function like !function(){return true};
soit will convert the true
to false
.
Reference1 ,
Reference2