Tutorial by Examples

function sum(numbers) { var total = 0; for (var i = numbers.length - 1; i >= 0; i--) { total += numbers[i]; } return total; } It's a procedural code with mutations (over total).
function sum(numbers) { if(numbers.length == 0) { return 0; } return numbers[0] + sum(numbers.slice(1)); } this is the recursive version. there's no mutation, but we are making a call stack as below which uses extra memory. sum([10, 5, 6, 7]);      10 + sum([5, 6, 7]); ...
function sum(numbers) { return tail_sum(numbers, 0); } function tail_sum(numbers, acc) { if(numbers.length == 0) { return acc; } return tail_sum(numbers.slice(1), acc + numbers[0]); } in the tail recursive version, function return value does not need to wait till...

Page 1 of 1