return false the same as return?

Javascript

Javascript Problem Overview


Is

return false 

the same as:

return

Javascript Solutions


Solution 1 - Javascript

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 operators in JS make it seem like the might return the same thing, but they return objects of different types.

Solution 2 - Javascript

No, return; is the same as return undefined;, which is the same as having a function with no return statement at all.

Solution 3 - Javascript

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

Solution 4 - Javascript

Nope, one returns false, the other undefined.

See this JSFiddle

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

function fn2(){
    return;
}

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

At this JSFiddle

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

Solution 5 - Javascript

It's returning undefined it's commonly used to break execution of the following lines in the function

Solution 6 - Javascript

No, I do not think so. False is usually returned to indicate that the specified action the function is supposed to do has failed. So that the calling function can check if the function succeeded.

Return is just a way to manipulate programming flow.

Solution 7 - Javascript

No.

Test it in firebug console (or wherever) -

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

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

alert((function(){return;})()); //alerts undefined

Note that if you (even implicitly) cast undefined to boolean, such as in if statement, it will evaluate to false.

Related interesting read here and here

Solution 8 - Javascript

Its undefined

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

And yes in javaScript return is such a powerful stuff if used nicely in patters. You can return all the way to [] array, {} object to functions too.

Returning "this" you can go ahead and get implementation of class based objects and prototype inheritance and all.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhillip SennView Question on Stackoverflow
Solution 1 - JavascriptBob FincheimerView Answer on Stackoverflow
Solution 2 - Javascriptgen_EricView Answer on Stackoverflow
Solution 3 - JavascriptMichael BerkowskiView Answer on Stackoverflow
Solution 4 - JavascriptJames WisemanView Answer on Stackoverflow
Solution 5 - JavascriptTeneffView Answer on Stackoverflow
Solution 6 - Javascriptuncaught_exceptionsView Answer on Stackoverflow
Solution 7 - JavascriptAmol KatdareView Answer on Stackoverflow
Solution 8 - JavascriptAnil NamdeView Answer on Stackoverflow