62

Is

return false 

the same as:

return
Phillip Senn
  • 46,771
  • 90
  • 257
  • 373

8 Answers8

46

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.

Bob Fincheimer
  • 17,978
  • 1
  • 29
  • 54
39

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

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • 1
    Not necessarily. [Try](http://jsfiddle.net/josh3736/ZqzH2/) `var undefined='oops'; return undefined;`. – josh3736 Apr 08 '11 at 15:01
  • 4
    @josh3736: WTF? You can name a variable `undefined`. Who would do that? – gen_Eric Apr 08 '11 at 15:07
  • 2
    Worse, you could assign the *global* `undefined='oops'`, so that `function() { return undefined; }` would return 'oops'. Why? Who knows, but you need to be prepared for the possibility, especially if you're developing a library to be used by other people. The important thing to realize is that [the token `undefined` is different from the primitive value *undefined*.](http://stackoverflow.com/questions/4225968/json-parse-error-using-jquery-parsejson/4226440#4226440) (However, ECMAScript 5 does prohibit writing to `undefined`.) – josh3736 Apr 08 '11 at 15:17
  • @josh3736: So, they let you name a variable `undefined`, so you can screw with people? – gen_Eric Apr 08 '11 at 15:30
  • 14
    @Rocket yes. now sneak `undefined = true` into the jQuery source and watch half the internet collapse. – Raynos Apr 08 '11 at 16:39
  • 1
    @josh3736, @Raynos: There is still a solution to this problem that looks like that: `(function(undefined){/** undefined is really undefined here **/})();` (see [working jsfiddle](http://jsfiddle.net/gqVzA/)). – Tadeck Dec 30 '11 at 22:10
  • @Tadeck: You can also do `var undef;` and then use `undef` instead of `undefined`. – gen_Eric Dec 30 '11 at 22:14
  • Rocket: Good point! It solves the problem with ECMAScript5 mentioned by @josh3736 :) – Tadeck Dec 30 '11 at 22:18
  • @Tadeck: Actaully, he said ECMAScript5 fixes the problem :-P – gen_Eric Dec 30 '11 at 22:19
9

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

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
5

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/

James Wiseman
  • 29,946
  • 17
  • 95
  • 158
  • That's interesting. I also added function fn3(){} and it alerted as well. But apparently, best practice is to explicitly say return false. – Phillip Senn Apr 08 '11 at 14:28
5

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
uncaught_exceptions
  • 21,712
  • 4
  • 41
  • 48
5

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

Teneff
  • 30,564
  • 13
  • 72
  • 103
3

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

Amol Katdare
  • 6,740
  • 2
  • 33
  • 36
  • I have firebug installed and use it to inspect things, but have never used it as a kind of javascript notepad. How do I copy/paste into firebug? – Phillip Senn Apr 08 '11 at 14:30
  • @cf_PhilipSenn - Open firebug, go to console. There you will either see a "command line" (marked by >>>) or if you have "show large command line" console option set, you will see it as a text area in the right side (in console tab). Paste one of the statements above and hit Enter (for small command line) or click on Run (large command line) – Amol Katdare Apr 08 '11 at 15:20
2

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.

Anil Namde
  • 6,452
  • 11
  • 63
  • 100