關(guān)于函數(shù)返回值

if條件判斷 里邊 return false; 或者return true;并不能作為函數(shù)的返回值。if條件判斷里的return,只是影響程序是否繼續(xù)執(zhí)行。
如果需要返回值,需要在函數(shù)層級 return 下
另外,可能牽扯到?jīng)]有涉及到的情況,需要有個默認(rèn)值。

<!DOCTYPE html>
<html lang="en">
<style>
    div{
        width: 100px;
        height: 50px;
        background-color: skyblue;
        margin: 40px;
        text-align: center;
        line-height: 50px;
    }
</style>
<body>
<div onclick="tag()">
   按鈕
</div>
<script>
let aa = 'zifuchuanzifuchuan';
let bb = 'zifuchuanzifuchuan';
function tag(){
    console.log('執(zhí)行了嗎')
    console.log('函數(shù)執(zhí)行結(jié)果',compare(aa,bb)) //undefined
   console.log('函數(shù)執(zhí)行結(jié)果',compareVal(aa,bb))  // true
  if(!compare(aa,bb)){
    return 
  }
  console.log('還執(zhí)行嗎')
}

 function compare(a,b){
    if(a.length < b.length){
         return false
    }
 }

 function compareVal(a,b){
    let compareFlag = true;
    if(a.length < b.length){
        compareFlag = false
    }
    return compareFlag
 }
</script>
</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容