return false the same as return?

前端 未结 8 1528
走了就别回头了
走了就别回头了 2020-12-01 03:33

Is

return false 

the same as:

return
相关标签:
8条回答
  • 2020-12-01 03:48

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

    0 讨论(0)
  • 2020-12-01 03:48

    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/

    0 讨论(0)
  • 2020-12-01 03:51

    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

    0 讨论(0)
  • 2020-12-01 03:53

    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.

    0 讨论(0)
  • 2020-12-01 03:57

    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.

    0 讨论(0)
  • 2020-12-01 03:59

    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.

    0 讨论(0)
提交回复
热议问题