Here's a more concise approach that has no side effects:
function r(arr, acc) { acc = acc || null; return arr ? r(arr[1], [arr[0], acc]) : acc; }
http://jsfiddle.net/5b4xntwg/1/
It makes the following recursive calls to enter [1, [2, [3, null]]] :
r([1, [2, [3, null]]] ) r([2, [3, null]] , [1, null] ) r([3, null] , [2, [1, null]] ) r(null , [3, [2, [1, null]]])
The last call to arr has null (this is the base case), so it just returns acc , which has the value [3, [2, [1, null]]] .
It should be mentioned that this structure of nested arrays is basically a list of minuses , which is widely used in functional programming and is very conducive to recursive operations.
Finally, here is the iterative version:
function r(arr) { var acc = null; while (arr) { acc = [arr[0], acc]; arr = arr[1]; } return acc; }