IF的声明搞错了



嘿,我是javascript的初学者,遇到了这个问题。IF语句似乎将变量识别为非变量。我认为这与OR运算符有关?

let variabel = `a variable with random value`;
if (variabel === `a` || `b`){
console.log(`the variable is the same as a or b`)
} else {
console.log(`not the same`)
}

所以它确实在控制台中显示the variable is the same as a or b。那么if语句是真的吗?但事实并非如此?

正确的语法是:

let variabel = `a variable with random value`;
if (variabel === `a` || variabel === `b`){
console.log(`the variable is the same as a or b`)
} else {
console.log(`not the same`)
}

正确使用OR是通过这种方式

let variabel = `a variable with random value`;
if (variabel === `a` || variabel === `b`){
console.log(`the variable is the same as a or b`)
} else {
console.log(`not the same`)
}

相关内容

  • 没有找到相关文章

最新更新