Iterating over an object with a for-in loop in JavaScript

The loop for-in is intended to iterate over objects. It has the following syntax:

for (let variableForKey in object) { }

In variableForKey the keys of the iterated object will fall sequentially. Let's try with some example. Suppose we have such an object:

let obj = {a: 1, b: 2, c: 3};

Let's use the loop for-in to derive the keys of this object:

for (let key in obj) { console.log(key); // shows 'a', 'b', 'c' }

Now let's get the elements:

for (let key in obj) { console.log(obj[key]); // shows 1, 2, 3 }

Print all the keys of the following object to the console:

let obj = {x: 1, y: 2, z: 3};

Print all the elements of the following object to the console:

let obj = {x: 1, y: 2, z: 3};
enru