非法返回语句错误

Illegal return statement error

本文关键字:错误 语句 返回 非法      更新时间:2023-09-26

我的返回语句有什么问题?

var creditCheck = function (income) {
    var val = income;
    return val;
};
if (creditCheck > 100) {
    return "You earn a lot of money! You qualify for a credit card.";
} else {
    return "Alas, you do not qualify for a credit card. Capitalism is cruel like that.";
}
console.log(creditCheck(75));

您的return语句在任何函数之外。 只能在函数中使用return

(您还在if (creditCheck > 100)中将函数与整数进行比较 - 您的意思是在那里调用该函数吗?

我在下面对您的问题进行了一些澄清。希望对您有所帮助。

var income = 50;//首先,您需要申报我在本例中设置为 50 的收入//

然后,您需要将信用检查声明为收入的函数。请注意,return 仅在函数中起作用。要在函数外部打印到控制台,请使用 console.log()//

var creditCheck = function (income) {
  if (income > 100) {
    return "You earn a lot of money! You qualify for a credit card.";} 
    else {
        return "Alas, you do not qualify for a credit card. Capitalism is cruel like that.";
    }
};
creditCheck(income); //You can execute the function by calling it.//

下面的文本显示了执行函数时打印到控制台的内容//

"唉,你没有资格获得信用卡。资本主义是残酷的 那个。

重新缩进和简化代码会显示:

var creditCheck = function(income) {
    return income; // essentially a no-op
};
if( creditCheck > 100) { // if a function is greater than a number?
    return "You earn a lot...";
    // is this code in a function? Otherwise it's an invalid return!
}
// else is unnecessary, due to `return` above.
return "Alas, you lack basic JavaScript knowledge...";
// console.log is never reached due to `return`.

查看评论 - 有很多错误!

if (creditCheck > 100) {
    return "You earn a lot of money! You qualify for a credit card.";
} else {
    return "Alas, you do not qualify for a credit card. Capitalism is cruel like that.";
}

这两个返回都是无效的,因为它们不在函数中。

(creditCheck> 100) 是无效的,因为 credicheck 是一个函数,需要提供一个变量来返回任何内容

var creditCheck = function (income) {
    return income;
};
if (creditCheck(50) > 100) {
    console.log("You earn a lot of money! You qualify for a credit card.");
} else {
    console.log("Alas, you do not qualify for a credit card. Capitalism is cruel like that.");
}

会添加 唉,您没有资格获得信用卡。资本主义就是这样残酷的。到控制台日志

下载 http://www.helpmesh.net/s/JavaScript/javascript.chm以获取javascript的基本语法,您将节省大量时间。你遇到的那种问题,语法,不是stackexchange的创建目的。