var reverse = function (list) {
return list.reduce(function (reversedList, current) {
return [ current ].concat(reversedList);
}, []);
};
console.log(reverse([1,2,3,4]));
So this is for reversing an array in Javascript using reduce. According to MDNs ref. The second argument(here current) is the second element after the first element of the array if no initialValue is provided. But in this case, the current is not the second element but the last element of the array. Why is it so?
You can run this code on the console for some array [1,2,3,4] and current will be returning 4.