Vasyl Kozhushko
Vasyl Kozhushko

Reputation: 83

"or" condition doesn't work JavaScript

I have a condition which should predict run the function. but it wouldn't work.

if ((event.keyCode !== 40) || (event.keyCode !== 38)) {
        self.inputVal = input.value;
        var result = pokemonNames.filter(function(pokemon) {
          return pokemon.includes(self.inputVal.toLowerCase());
        });
        if (result.length > 5) {
          result = result.filter(function(pokemon, index) {
            return index < 5;
          });
        }

when I use just event.keyCode !== 40 it works fine, but when I use (event.keyCode !== 40) || (event.keyCode !== 38) it would work.

Upvotes: 0

Views: 70

Answers (2)

As Aaron pointed out in the comments, it will always resolve as true. If the number is 40, the second condition will be true (since it's not 38), and vice versa. Use && instead and it will work.

Upvotes: 0

Jason H
Jason H

Reputation: 5156

If you want to execute the if block if the event.keyCode is NOT 40 or 38 you want this

((event.keyCode !== 40) && (event.keyCode !== 38))

If you want to execute the code if event.keyCode is 40 or 38

((event.keyCode == 40) || (event.keyCode == 38))

Upvotes: 1

Related Questions