const person = {
    name: "John",
    age: 30,
    occupation: "developer"
};

for (const key in person) {
    console.log(`${key}: ${person[key]}`);
}
console에서 출력된 모습
const iterable = [1, 2, 3, 4, 5];

for (const item of iterable) {
    console.log(item);
}
console에서 출력된 모습
const person = {
    name: "Alice",
    age: 25,
    occupation: "teacher"
};

for (const key in person) {
    console.log(`${key}: ${person[key]}`);
}
console에서 출력된 모습
const colors = ["red", "green", "blue"];

for (const index in colors) {
    console.log(index); // 0, 1, 2 (인덱스)
}
const numbers = [1, 2, 3, 4, 5];

for (const number of numbers) {
    console.log(number); // 1, 2, 3, 4, 5
}
const message = "Hello";

for (const char of message) {
    console.log(char);
}
console에서 출력된 모습
const person = {
    name: "Bob",
    age: 30,
    occupation: "engineer"
};

for (const key in person) {
    const value = person[key];
    console.log(`${key}: ${value}`);
}
console에서 출력된 모습
const numbers = [5, 10, 15];

for (const number of numbers) {
    console.log(number); // 5, 10, 15
}
const message = "Hello";

for (const char of message) {
    console.log(char);
}
console에서 출력된 모습
const colors = ["red", "green", "blue"];

for (const index in colors) {
    console.log(index); // 0, 1, 2 (인덱스)
}
function Person(name) {
    this.name = name;
}

Person.prototype.age = 30;

const person = new Person("Alice");

for (const key in person) {
    console.log(`${key}: ${person[key]}`);
}
console에서 출력된 모습
Array.prototype.customMethod = function() {
    console.log("This is a custom method.");
};

const numbers = [10, 20, 30];

console.log("Using for...of:");
for (const number of numbers) {
    console.log(number);
}

console.log('Using for...in:');
for (const index in numbers) {
    console.log(index); // 'customMethod'도 출력됨
}
console에서 출력된 모습
for...in 문과 for...of 문의 브라우저 호환성
데스크탑 Chrome
Chrome
데스크탑데스크탑 Edge
Edge
데스크탑 Firefox
Firefox
Safari
Safari
for...in 1 12 1 1
for...of 38 12 13 7

caniuse.com에서 더 자세한 정보를 확인해 보세요.