Please someone help me to understand the following basic JS concept

Hi there,

Here is my following code.

const animal = ["cat", "dog", "lion", "tiger"];\

for (let i=0; i<animal.length; i++){
  if(animal[i]==="lion" || "tiger"){
    let animals = animal[i];
} // "cat" "dog" "lion" "tiger"

The output was “cat” “dog” “lion” “tiger”

I’m just assigning the variable animals only when the index value of array is equal to (“lion” or “tiger”) but why I’m getting the output of the whole array;

Please will someone can explain me why its happening and If I’m wrong please tell me how to assign the variable to only the specific values in the array (like two or three values by using OR )

That doesn’t work the way you think it does.

It is equivalent to

(animal[i] === "lion") || ("tiger")

but the string "tiger" is truthy, so your condition always evaluates to true and the code inside the if statement will always run.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.