2

I have the following two codes set. The first alerts 'true', I know that, Its Ok. The second one alerts 'false', I thought that would alert '!function(){return false}'. But, It alerts 'false'. How this second code block alerts false? Could anyone help me understand that please?

var myVar = !function(){return false}();
alert(myVar);   //true 
var myVar = !function(){return false};
alert(myVar);  //false, I dont understand this, how false can come?
User123
  • 443
  • 5
  • 14

3 Answers3

1

First snippet

var myVar = !function(){ return false; }( );
// myVar = false; because invocation ----^
alert(myVar); // !false = true

Second snippet

var myVar = !function(){return false};
// myVar = Function myVar = !(Function) and a Function is *truthy*
alert(myVar); // !true  = false

In the first snippet, you're calling it right away, so myVar is false and !false is obviously true.

In the second snippet however, you ain't calling the function. Instead, myVar is now a function, which is truthy. That is, it's considered true. So again !true = false.

Remember, in javascript, false, NaN, null, undefined, 0 , "" are falsey and everything else is truthy.

Amit Joki
  • 53,955
  • 7
  • 67
  • 89
0

You should avoid the !function statement. when you use the () at the end of a function, you actually say you want to use the return output of the function. you didn't use it with the last statement.

var myVar = function(){return true}();
alert(myVar);   //true 
var myVar = function(){return false}();
alert(myVar);  //false
Jari
  • 30
  • 5
0

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

Community
  • 1
  • 1
Arunprasanth K V
  • 17,147
  • 8
  • 33
  • 63
  • It has nothing to do with myVar containing a function already. Even without the () at the end, the ! turns the second example from a function declaration to a function expression because JavaScript thinks it needs to find the inverse of something. See http://stackoverflow.com/questions/13341698/javascript-plus-sign-in-front-of-function-name – Dave Ross Mar 03 '15 at 12:45
  • @DaveRoss thanks for your valid suggestions i had updated my answer – Arunprasanth K V Mar 03 '15 at 12:58