What is this kind of assignment?

let [prev, curr] = [0, 1];

that is called array destructuring, the [0, 1] array items are destructured in the variables to the left

1 Like

so that is what is happening below?

for (let [key, value] of Object.entries(yourobject)) {
    console.log(key, value);
}

Yes. Object.entries returns an array containing subarrays of two elements which are keys/values of the object. The for loop iterates through the outer array and the destructuring lets you easily access/name the sub array elements during each iteration.

It would be the same as writing:

for (let entry of Object.entries(yourobject)) {
    console.log(entry[0], entry[1]);
}

OR

for (let entry of Object.entries(yourobject)) {
    const key = entry[0];
    const value = entry[1];
    console.log(key, value);
}

Using destructuring makes the code much more readable with very little syntax.

1 Like