How can you write such an article and not talk about tail recursion? Writing that these are just “learning exercises” is not good enough. A good teacher always shows a bad example and then explains why it is bad then gives the good solution.
All your examples are bad because they require the end of the recursion to be reached before unwinding the call stack. And this may cause the call stack to be unnecessarily huge!
Consider the following example. Note that no unwinding of the call stack is needed (thanks to the use on an accumulator), hence greatly improving the memory usage and stability of your program.
```js
function reverse ( str ) { return reverse_sub( str, “” );
function reverse_sub ( str, acc ) { if ( str.length === 0 ) { return acc; }
return reverse_sub( str.substr( 1 ), str[0] + acc ); }
}
```