admin管理员组文章数量:1426347
How using multiple conditions within an if statement?
function testNum(a) {
if (a == (1 || 2 || 3)) {
return "is 1, 2, or 3";
} else {
return "is not 1, 2, or 3";
}
}
console.log(testNum(1)); // returns "is 1, 2, or 3"
console.log(testNum(2)); // returns "is not 1, 2, or 3"
console.log(testNum(3)); // returns "is not 1, 2, or 3"
How using multiple conditions within an if statement?
function testNum(a) {
if (a == (1 || 2 || 3)) {
return "is 1, 2, or 3";
} else {
return "is not 1, 2, or 3";
}
}
console.log(testNum(1)); // returns "is 1, 2, or 3"
console.log(testNum(2)); // returns "is not 1, 2, or 3"
console.log(testNum(3)); // returns "is not 1, 2, or 3"
testNum(2)
and testNum(3)
should return: "is 1, 2 or 3"
but doesn't.
- (a == (1 || 2 || 3)) is invalid syntax – Kevin Vandy Commented Apr 8, 2019 at 17:55
- 1 @KevinVandy How is that? No syntax errors in the code. – Teemu Commented Apr 8, 2019 at 17:57
- @KevinVandy It's not at all (see my answer)! – Joe Iddon Commented Apr 8, 2019 at 17:59
- @Teemu Ok, correction, it is misleading syntax – Kevin Vandy Commented Apr 8, 2019 at 17:59
- 1 @KevinVandy it is a logical error, not a syntax error. – Isaac Vidrine Commented Apr 8, 2019 at 18:01
6 Answers
Reset to default 8In this particular scenario, you can even use an array and Array#includes
method for checking.
if ([1, 2, 3].includes(a)) {
// your code
}
function testNum(a) {
if ([1, 2, 3].includes(a)) {
return "is 1, 2, or 3";
} else {
return "is not 1, 2, or 3";
}
}
console.log(testNum(1));
console.log(testNum(2));
console.log(testNum(4));
console.log(testNum(3));
FYI : In your current code
(1 || 2 || 3)
results 1
(since 1 is truthy) and actually a == (1 || 2 || 3)
does a == 1
. The right way is to seperate each conditions with ||
(or), for eg : a == 1 || a == 2 || a ==3
.
For more details visit MDN documentation of Logical operators.
You cannot have ||
like that. The one you have used is not the right way. You should be using:
function testNum(a) {
if (a == 1 || a == 2 || a == 3) {
return "is 1, 2, or 3";
} else {
return "is not 1, 2, or 3";
}
}
console.log(testNum(1));
console.log(testNum(2));
console.log(testNum(3));
Your or-operators are placed incorrectly:
function testNum(a) {
if (a == 1 || a == 2 || a == 3) {
return "is 1, 2, or 3";
} else {
return "is not 1, 2, or 3";
}
}
Before, you were testing if a
was equal to 1 || 2 || 3
which evaluates to 1
†. So you were just checking a == 1
which is not what you wanted!
† Essentially when you string together "or"s like this, the first truthy value is returned. For example, you can assert for yourself that: 0 || False || 5
gives 5
.
You can try using a list of qualified values and check if the lookup value belongs there:
function testNum(a) {
var candidates = [1, 2, 3];
if (candidates.indexOf(a) != -1) {
return "is 1, 2, or 3";
} else {
return "is not 1, 2, or 3";
}
}
console.log(testNum(1)); // returns "is 1, 2, or 3"
console.log(testNum(2)); // returns "is not 1, 2, or 3"
console.log(testNum(3));
console.log(testNum(4));
You can either
if (a == 1 || a == 2 || a == 3) {
or
if ([1, 2, 3].includes(a)) {
which will be more convenient when there's more values to test. If you want to be patible with ancient browsers, you have to write it like this
if ([1, 2, 3].indexOf(a) >= 0) {
which look undeniably uglier.
The reason why you are not getting the expected result because you are performing a Logical OR operation here.
As per MDN Network : Logical OR (||) expr1 || expr2 If expr1 can be converted to true, returns expr1; else, returns expr2.
(1 || 2 || 3) is evaluated and the operation results at 1. You can check the result by pasting (1 || 2 || 3)
in Chrome debugger or something.
So when the first number is 1 in all cases, the expression result is also 1. You can change the expression as how others have advised to get the desired output.
本文标签: javascriptUsing multiple conditions within an if statementStack Overflow
版权声明:本文标题:javascript - Using multiple conditions within an if statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745446937a2658705.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论