Is there a difference between a function with and without a return statement?

后端 未结 3 1054
小鲜肉
小鲜肉 2021-01-18 12:41

Say you have 2 identical functions that do not return a value

function a() {
    // do some interesting things
}

function b() {
    // do the same interesti         


        
3条回答
  •  不思量自难忘°
    2021-01-18 13:12

    There's no real difference; both will return undefined.

    Functions with no return statement will return undefined, as will functions with an empty return statement.


    To confirm this for yourself, you can run this code -- FIDDLE:

    ​function a() {
    }
    
    function b() {
        return;
    }
    
    var aResult = a();
    var bResult = b();
    
    alert(aResult === bResult);  //alerts true
    

提交回复
热议问题