JavaScript: 为什么 boolean 总是返回 true

JavaScript: why is boolean always returning true

当我 运行 使用 customerNumber 的任何值时,它总是 returns if 语句中的第一个警告消息。这里有什么问题?

var customerNumbers = 13;
var winningNumbers = [];
winningNumbers.push(12, 17, 24, 37, 38, 43);
var match = false;
for (i=0; i<winningNumbers.length; i++) {
    if (winningNumbers[i] == customerNumbers) {
        match = true;
    }
}
if (match = true) {
    alert("This Week's Winning Numbers are:\n" + winningNumbers.toString() + 
    "\nThe Customer's Number is:\n" + customerNumbers + "\nWe have a match and a winner!");
}
else {
    alert("This Week's Winning Numbers are:\n" + winningNumbers.toString() + 
    "\nThe Customer's Number is:\n" + customerNumbers + "\nSorry, you are not a winner this week.");
}

不要在 if 语句中使用赋值运算符(=)。使用相等运算符 (===).

if (match === true)

您没有将布尔值 matchtrue 进行比较,您正在将 true 分配给布尔值 match。您需要使用双倍或三倍 =.

match = true // Sets match to true
match == true // Compares match to true
match === true // Strictly compares match to true

所以再添加一些等号,它应该可以工作。