'True' 在定义之前被使用

'True' was used before it was defined

我有一个 JavaScript 错误提示:'True' was used before it was defined。 我有这个代码:

var playAgain = window.confirm("Play again? OK for yes, Cancel for no.");
if (playAgain === True) {
    loop();
}

我想让它没有那个错误。

在 JavaScript 中,布尔值的正确写法是 true。如果您尝试做的是定义和使用名为 True 的变量,请考虑定义它(例如 const True = true)。

True是一个变量,在使用前需要声明。但是您期望的是 true,其中 data type in javascript。由于 JS 区分大小写,因此 Truetrue.

不同

True 替换为 true

const True = "I am a variable";
console.log(True);

您不能使用 true 作为变量名。

const true = "Error";   // SyntaxError: Unexpected token 'true'
console.log(true);