在 JavaScript(和大多数其他编程语言)中,我注意到在检查同一变量的多个条件并为每个条件执行相同操作时,很难简洁地编写 if 语句。在这种情况下是否可以更简洁地编写 if 语句?
if(x==1|x==2|x==3){ //Is there any way to make this less verbose?
console.log("X equals either 1 or 2 or 3!");
}
//this isn't syntactically correct, but it's more concise,
//and I wish it were possible to write it more like this
if(x==(1|2|3)){
console.log("X equals either 1 or 2 or 3!");
}