简体   繁体   English

return 在 if 语句中抛出错误

[英]return throws error in if statement

I have an if statement with return s in it that throw an error when executed.我有一个带有returnif语句,它在执行时抛出错误。

var booleanStatement = true;

if ( booleanStatement ){
    return true;
} else {
    return false;
}

This should return true but it instead throws an error.这应该返回 true 但它会引发错误。 How can I get this statement to return true?我怎样才能让这个语句返回true?

You need a function for using the statement return .您需要一个函数来使用语句return

The return statement ends function execution and specifies a value to be returned to the function caller. return语句结束函数执行并指定要返回给函数调用者的值。

 var array = [0, 1, 2, , 4, 5]; function x() { if (array[3] === 3) { return true; } else { return false; } } console.log(x());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM