for (variable in object) {
    // 실행할 코드
}
const person = {
    name: "John",
    age: 30,
    occupation: "Developer"
};

for (let key in person) {
    console.log(key); // 프로퍼티 이름 출력
}
console에서 출력된 모습
const person = {
    name: "John",
    age: 30,
    occupation: "Developer"
};

for (let key in person) {
    console.log(key + ": " + person[key]); // 프로퍼티 이름과 값 출력
}
console에서 출력된 모습
const parent = {
    parentProp: "I am from parent"
};

const child = Object.create(parent);
child.childProp = "I am from child";

for (let key in child) {
    console.log(key); // parentProp, childProp 출력
}

for (let key in child) {
    if (child.hasOwnProperty(key)) {
        console.log(key); // childProp만 출력
    }
}
const obj = {
    enumerableProp: "I will be enumerated",
};

Object.defineProperty(obj, "nonEnumerableProp", {
    value: "I will not be enumerated",
    enumerable: false
});

for (let key in obj) {
    console.log(key); // enumerableProp만 출력
}
for (let key in object) {
    if (object.hasOwnProperty(key)) {
        // 객체 자체의 프로퍼티에 대한 작업 수행
    }
}
Object.defineProperty(object, "nonEnumerableProperty", {
    value: "This property is not enumerable",
    enumerable: false
});
const obj = null;

for (let key in obj) {
    // 이 부분은 실행되지 않음
}
const person = {
    name: "John",
    age: 30,
    occupation: "Developer"
};

const keys = Object.keys(person);
const values = Object.values(person);
const entries = Object.entries(person);

console.log(keys);    // ['name', 'age', 'occupation']
console.log(values);  // ['John', 30, 'Developer']
console.log(entries); // [['name', 'John'], ['age', 30], ['occupation', 'Developer']]
const values = Object.values(person);

for (const value of values) {
    console.log(value);
}
const person = {
    name: "John",
    age: 30,
    occupation: "Developer"
};

const entries = Object.entries(person);

entries.forEach(([key, value]) => {
    console.log(key + ": " + value);
});
for...in 문에 대한 브라우저 호환성
데스크탑 Chrome
Chrome
데스크탑데스크탑 Edge
Edge
데스크탑 Firefox
Firefox
Safari
Safari
for...in 1 12 1 1

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