# Question about using for loops on an array

Question about using for loops on an array
0

How does Javascript know for sure exactly which arrays elements (in this case arr1 or arr2) you are iterating on? Because the for statement doesn’t exactly specify. Is it the part where we specify the stopping condition in this example arr2.length, that tells javascript to loop through arr2 instead of arr1? Or does the for loop loop through both and it depends on what we tell the for loop to do?

``````function mutation(arr) {
let arr2 = arr[1].split('')
let arr1 = arr[0].split()
let check = ''

for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf([arr2[i]].includes(-1))) {
return false
} else {
return true
}
}
}``````

My take on this:

According to Cambridge Dictionary
If something runs IN a loop, it runs continuously so that the same things are repeated again and again.

For me, it’s not about the construct of the loop, it’s about what you’re doing within the loop that makes it a loop.

The construct from the presented example is

``````for (i = 0; i < arr2.length; i++)
``````

just helps to set a boundary for your loop and provide an iterator, which when used on any iterable subjects such iterable to a loop.

Based on my above point, Javascript will only run computation based on the statements within the loop construct while at same time, observe the construct to know when to stop.

HERE

From your example, Javascript will run computation on the arr1 and arr2 simply because the both of them are used WITHIN the loop construct AND are being affected by the iterator i (Very important) and Irrespective of the length of arr2 (which is used as a boundary to stop the loop), it’s likely that your loop will end quickly because of the return statement.

For loop knows which array because you specify to it where to loop so in this case you told for loop to go through `arr2`: `i <arr2.length`. I guess you re confused by the fact that it checks if an element of another array is in the other, well in general some methods they are loops themselves, they wouldn t know if an element is included somewhere else so behind the scenes they iterate and check and return a value eventually. I m not sure why in your code are both: `indexOf` and `includes` you only need one of them and based on its return value you can decide what to return. Actually `includes` itself returns true or false so you can simplify your condition even more.

neither. the loop iterate over `i`, everything else depends on how you use the `i`
so in your loop you have `arr2[i]` so it means it is looping over `arr2`

note this is not doing anything you may think it is doing.
you are checking the index inside `arr1` of `[arr2[i]].includes(-1)` - includes returns `true` or `false` (and it returns true only if `arr2[i]` equals `-1`), and the arrays mostly do not include booleans, so your idnexOf will most often than not returns `-1` which is a truthy value, and so your `return false` will execute
also, because you have an if/else inside the loop and each one have a return statement your `i` will stop at value of 0

plus you have never declared the `i`, you should if you don’t want an error about using undeclared variables