var permute_list = []; function permute(_arr, _temp) { if (_temp.length === _arr.length) { permute_list.push(_temp.slice()); return; } _arr.forEach(num => { if (!_temp.includes(num)) { _temp.push(num); permute(_arr, _temp); _temp.pop(); } }); } permute([0, 1, 2], []); console.log(permute_list); /* [ [ 0, 1, 2 ], [ 0, 2, 1 ], [ 1, 0, 2 ], [ 1, 2, 0 ], [ 2, 0, 1 ], [ 2, 1, 0 ] ] */