for...in문은 객체에서 문자열로 키가 지정된 모든 열거 가능한 속성에 대해 반복합니다.
array도 for...in 문을 이용하여 적용이 가능하지만,
앞서 정의된 문자열로 키가 지정된다는 특성 때문에 index 값의 type이 문자열이 됩니다.
array를 사용 시 index의 type을 그대로 두고싶다면 일반적인 for 문을 사용합니다.
function iter(collection) {
for(el in collection){
console.log(el);
}
}
function iterArray(arr){
for(let i = 0 ; i < arr.length ; i++){
console.log(i);
}
}
const arr = ['a', 'b', 'c'];
const obj = {a : "apple", b : "banana", c : "cat"};
console.log(typeof arr);
> "object"
console.log(typeof obj);
> "object"
console.log(Array.isArray(arr));
> true
console.log(Array.isArray(obj));
> false
iter(arr);
> "0" "1" "2"
iterArray(arr);
> 0 1 2
iter(obj);
> "a" "b" "c"
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Statements/for...in