Q:

For loop with object

I come across this code below which add a specified property value of all block of a nested object to an array called arrays.
My problem is that  I don' t understand how iteration works in a nested object like the one below using for loop.
Can someone explain to me how for loop iteration is possible with an object?
let list = {
   value: 1,
   rest: {
      value: 2,
      rest: {
         value: 3,
         rest: null
    }
  }
};

below is the function that  does the job
function listToArray(list) {
  let arrays = [];
  for (let node = list; node; node = node.rest) { // big question: how it works or possible?
    arrays.push(node.value);
  }
  return arrays;
}
console.log(listToArray(list)); // -> [1, 2, 3]

A:

How do for loops work:
First you need to understand how for loops works.

The for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement) to be executed in the loop. MDN

The for loop has this structure:
for ([initialization]; [condition]; [final-expression])
   statement

The expressions initialization, condition and final-expression could be any valid expression. Any or all of these expressions could be omitted (for(;;) is valid in javascript and it creates an infinite loop).
The for loop begins by executing the initialization expression if specified, then repeats the following actions: check if the condition is truthy, if so then it executes statement(s) and final-expression in order, if the condition is falsy it stops looping.
Here is an example, the following diagram (image source):

Corresponds to the follwing for loop:
for(i = 2; i <= 6; i = i + 2)
  print i + 1

Notice that the initialization part i = 2 is only executed once whereas the rest (the condition part i <= 6, the statement(s) part and the final-expression i = i + 2) could be executed multiple times (in order) depending on the condition.
Explanation of the code in question:
for (let node = list; node; node = node.rest) { // big question: how it works or possible?
  arrays.push(node.value);
}

The initialization part of this loop just declares the variable node and sets its value to the root element list, this part is only executed once, when the loop is about to start.
The condition part checks whether node (the variable) is truthy or not, in javascript an object is truthy whereas undefined is not, it's a falsy value (there are other truthy and falsy values of course but we are only interested in these two for this particular example). The concept of the loop is running down the object from node to node (see the final-expression part bellow). When a child node exists, the next value of node will be that node (which is an object), that means it's gonna be a truthy value, hence the condition will be true. Whereas if the child node doesn't exists, node will be undefined (a falsy value) and thus the condition will be falsy.
The final-expression just sets the value of node to the child node of the current node which is node.rest. If it exists node will be an object, otherwise it will be undefined.

