Why (8 == (10 || 9 || 8)) === false, and what to replace this condition?

Hello all I can not understand what I'm doing wrong. Thank you in advance for the clarification.
June 8th 19 at 17:21
3 answers
June 8th 19 at 17:23
Solution
if 8 is not equal to 10 then the remaining conditions are not checked. there should be something on the similarity of in_array()
javascript.ru/php/in_array
June 8th 19 at 17:25
Solution
First, it marks the expression in parentheses (10 || 9 || 8)and it is 10 since Boolean(10) === true.

Then you can compare (8 == <previous result>), that is, 8 == 10, and it is equal to false.

Replace better comparison:
x === 1 || x === 2 || x === 3

or
x >= 1 && x <= 3

or if a more complex comparison, you can use the Set (better than an array):
let values = new Set(['215', '456', '766', ...]);

function some(x) {
 // ...
 if (values.has(x)) {
 // ...
}
}
June 8th 19 at 17:27
probably you need something like this:
if (![10, 9, 8].includes(8)) ...

Apparently you wanted to write
if ((8 == 10 || 8 == 9 || 8 == 8) === false) ...
In this case:

1) when comparing numbers better ispolzovat strict equality (===):
// for the adequacy of the example to replace 8 x
if ((x === 10 || x === 9 || x === 8) === false) ...


2) it makes no sense to compare the result of the logical operation is false, you can just put before the parenthesis !:
if (!(x === 10 || x === 9 || x === 8)) ...
The comparison with false was only for illustrative purposes. Nothing new for myself, I do not know, but thanks for the clarification:) - Henri_Donnel commented on June 8th 19 at 17:30
note that Set (on which he wrote below, and which really makes sense to use) belongs to es2015 standard (es6), and includes at all: es2016. And therefore can not be supported in all browsers - use politely, if needed.

includes can be replaced by:
if ([10, 9, 8].indexOf(8) === -1)... - idell_Kassulke commented on June 8th 19 at 17:33
thank you. Did in the end. - Henri_Donnel commented on June 8th 19 at 17:36

Find more questions by tags JavaScript