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`)
}