Welcome to SO!
If you want to loop through the array then you will need to use something besides for..in
. You can use for..of
or a for
loop but it might be good to understand why this is happening.
for..in
loops through the key/value pairs of an object. The variable in the parens gets set to the key
of that key/value pair. In the case of the array, It will get set to the index or any enumerable property on the array. Thats why it prints numbers instead of words.
I'd also like to point out some iteration methods that might be useful outside of imperative loops.
arr.forEach
will allow you to loop through an array without needing the extra for
syntax.
arr.forEach(item => console.log(item))
Other methods like arr.map
and arr.filter
will give you even more power as you begin to iterate through your list more.
const numbers = [1,22,11,18,16];
const add = a => b => a + b;
const isEven = number => number %2 === 0;
const biggerEvenNumbers = numbers
.map(add(1))
.filter(isEven) // [2,12]