Is false false the same as return?

Is an

return false 

same as:

 return 
+45
javascript
Apr 08 2018-11-11T00:
source share
8 answers

No.

var i = (function() { return; })();

i === undefined , which means that i == false && i == '' && i == null && i == 0 && !i

var j = (function() { return false; })();

j === false , which means that j == false && j == '' && j == null && j == 0 && !j

Weak statements in JS make it seem like they can return the same thing, but return objects of different types.

+36
Apr 08 2018-11-11T00:
source share

No, return; matches return undefined; , which coincides with a function without a return statement.

+26
Apr 08 2018-11-11T00:
source share

No. They are not the same. Returning false from the function returns boolean false , where void return returns undefined .

+6
Apr 08 2018-11-11T00:
source share

No, one returns false , the other is undefined .

See this JSFiddle

but if you check this without true or false , it will evaluate to true or false :

 function fn2(){ return; } if (!fn2()){ alert("not fn2"); //we hit this } 

In this JSFiddle

http://jsfiddle.net/TNybz/2/

+5
Apr 08 2018-11-11T00:
source share

It returns undefined , which is usually used to interrupt the execution of the following lines in a function.

+5
Apr 08 2018-11-11T00:
source share

No, I do not think so. False is usually returned to indicate that the specified action that the function should take has failed. Thus, the calling function can check whether the function is executed.

Returns are just a way to control the flow of programming.

+4
Apr 08 2018-11-11T00:
source share

No.

Test it in firebug console (or anywhere) -

 alert((function(){return;})() == false); //alerts false. alert((function(){return false;})() == false); //alerts true. alert((function(){return;})()); //alerts undefined 

Note that if you (even implicitly) lead undefined in boolean, for example, in an if statement, it will evaluate to false.

Related interesting read here and here

+3
Apr 08 2018-11-11T00:
source share

Its undefined

 console.log((function(){return ;})()) 

And yes, in javaScript, return is such powerful material if it is well used in putters. You can completely return to the [] array, {} to functions.

By returning "this", you can continue implementing objects based on classes and prototype inheritance and all.

+2
Apr 08 '11 at 15:08
source share



All Articles