Javascript IF语句出错了吗?
嘿,我是javascript的初学者,我遇到了这个问题。IF语句似乎将变量识别为它不是的东西。我想这和OR操作员有关Javascript IF语句出错了吗?,javascript,if-statement,or-operator,Javascript,If Statement,Or Operator,嘿,我是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`) } 因此它确实表示变量与控制台中的
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`)
}
因此它确实表示变量与控制台中的a或b相同。那么if语句是真的吗?但事实并非如此?正确的语法是:
let variabel=`a variable with random value`;
if(variabel===`a`| variabel====`b`){
log(`该变量与a或b`相同)
}否则{
console.log(`not same`)
}
正确的语法是:
let variabel=`a variable with random value`;
if(variabel===`a`| variabel====`b`){
log(`该变量与a或b`相同)
}否则{
console.log(`not same`)
}
正确使用或是这样的
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`)
}
正确使用或就是这样
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`)
}
if语句正在测试变量==='a'
或“b”是否为真。因为“b”为,所以条件的计算结果为true。表达式的计算结果为(variabel===='a`)|(`b`)
。请记住,在这种用例中,模板文本不会给您带来任何帮助。if语句正在测试variable=='a'
或“b”是否为true。因为“b”为,所以条件的计算结果为true。表达式的计算结果为(variabel===`a`)| |(`b`)
。请记住,在这个用例中,模板文本不会给您带来任何好处。好吧,新手错误。我觉得我会犯更多的错误。我觉得我会做得更多哈哈