Difference between using logical expression in includes or outside of that

38 Views Asked by At

Today I face a problem with understanding one basic thing:

What is the difference between using the expression in functions such as includes or indexOf and outside of that?

So

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes('Mango' && 'Apple'); 

versus

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.includes('Mango') && fruits.includes('Apple') ; 

I thought both of them behaved the same, but I saw some differences. Can anyone explain to me what is the difference between these two and what is the purpose of the first one if it is different?

I tried the first code but have some unexpected results (instead of a logical AND, it acts like a logical OR). I tried with the indexOf function it was the same when I expect -1 I get the value of the index of one of the elements.

const  fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.includes('a' && 'Apple')) // true
console.log(fruits.indexOf('a' && 'Apple'))  // 2

0

There are 0 best solutions below