嘿,我是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`)
}