Phillip Senn
Phillip Senn

Reputation: 47605

return false the same as return?

Is

return false 

the same as:

return

Upvotes: 62

Views: 21163

Answers (8)

billygoat
billygoat

Reputation: 21984

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.

Upvotes: 5

Amol Katdare
Amol Katdare

Reputation: 6760

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

Upvotes: 3

Anil Namde
Anil Namde

Reputation: 6608

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.

Upvotes: 2

Mutation Person
Mutation Person

Reputation: 30498

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/

Upvotes: 5

Teneff
Teneff

Reputation: 32158

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

Upvotes: 5

Bob Fincheimer
Bob Fincheimer

Reputation: 18056

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.

Upvotes: 46

Michael Berkowski
Michael Berkowski

Reputation: 270617

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

Upvotes: 9

gen_Eric
gen_Eric

Reputation: 227240

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

Upvotes: 39

Related Questions