0

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
Ivar
  • 6,138
  • 12
  • 49
  • 61
  • 1
    Just paste `'Mango' && 'Apple'` into a js console and check the output? – kevinSpaceyIsKeyserSöze Jan 16 '23 at 08:31
  • oh i understand now its not work as logical and between return of those includes it was used as guard operator so first false return and send the result to includes. in my case both was true (string a and string apple because both have value ) i get the true correct ? – payam shariat Jan 16 '23 at 09:06

0 Answers0