for (variable in object) {
    // 実行するコード
}
const person = {
    name: "John",
    age: 30,
    occupation: "Developer"
} // オブジェクトリテラルで定義されたオブジェクト

for (let key in person) {
    console.log(key); // プロパティ名を出力
}
コンソールに出力された結果
const person = {
    name: "John",
    age: 30,
    occupation: "Developer"
} // オブジェクトリテラルで定義されたオブジェクト

for (let key in person) {
    console.log(key + ": " + person[key]); // プロパティ名と値を出力
}
コンソールに出力された結果
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);
});