Set an output in the loop, than you can see how it works:
function arrayToList(array) {
var list = null
for(var i = 0; i < array.length; i++) {
list = {value: array[i], rest: list};
console.log(list);
console.log('\n');
}
return list;
}
arrayToList([10, 20, 30]);
/* Output
{ value: 10, rest: null }
{ value: 20, rest: { value: 10, rest: null } }
{ value: 30, rest: { value: 20, rest: { value: 10, rest: null } } }
*/
You have set list = null.
The first time in the loop, list is "{ value: 10, rest: null }"
-> "null" nested within.
The second time in the loop, list is "{ value: 20, rest: { value: 10, rest: null } }"
-> "{ value: 10, rest: null }" nested within.
For the last time, list is "{ value: 30, rest: { value: 20, rest: { value: 10, rest: null } } }"
-> "{ value: 20, rest: { value: 10, rest: null } }" nested within.
arrayToList()function?