I'd like to know why the following function works:
function foo(list){
var array = [];
array.push(list);
return array;
}
> foo([1,2,3])
[[1,2,3]]
while this one doesn't:
function foo(list){
var array = [];
return array.push(list);
}
> foo([1,2,3])
1
What's the difference between them?
If you look at the definition of the push method, it returns the length of the array after the push, not the array itself, that is why it is returning 1.
You are pushing an array with 3 elements to the new array, so in the new array you have an array as its content thus 1 is returned