Why can the code use animal instead of animals?

I have a question about JavaScript.
In the code below, I noticed that the argument of the function uses the variable “animal”, as in “animal === ‘elephant’”
However, the array was declared with the function “animals” not “animal,” so how does the script “know” that animal is an item in the array “animals”
I have noticed with with a few other pieces of code, such as where an array is declared as “numbers” and later a function is passed the argument “num”. But “num” and “animal” were never declared as variables by the code. How does this work and what are the rules around it?

const animals = [‘hippo’, ‘tiger’, ‘lion’, ‘seal’, ‘cheetah’, ‘monkey’, ‘salamander’, ‘elephant’];

const foundAnimal = animals.findIndex(animal => {
return animal === ‘elephant’;
});

1 Like

The variable is defined in the loop.

for (let animal = 0; animal < animals.length; animal++) {

}
1 Like