是什么导致javascript错误;应为标识符,而看到的却是else“;

What causes javascript error "Expected identifier and instead saw else"?

本文关键字:else 标识符 javascript 错误 是什么      更新时间:2023-09-26

错误消息:

"Expected identifier and instead saw else."

这是代码(所有Javascript):

// Write your function below.
// Don't forget to call your function!
var sleepCheck = function(numHours) {
    if (numHours >= 8); {
        console.log("You're getting plenty of sleep! Maybe even too much!");    }
    else {
        console.log("Get some more shut eye!");
    }
};
console.log(sleepCheck)

这里不应该有分号:

if (numHours >= 8); {

if语句结构应该如下所示:

if (condition) {
} else if (condtion2) {
} else {
}

看起来if语句的右括号后面有一个分号。