const RecPermute = (soFar, rest) => { if (rest.length === 0) { console.log('result', soFar); console.log('\n\n'); } else { for (let i = 0; i < rest.length; i++) { const remaining = rest.substring(0,i) + rest.substring(i+1); console.log('r', remaining) console.log('R', rest); console.log('SF', soFar); console.log('------'); RecPermute(soFar + rest[i], remaining); } } }; x = 'abc'; RecPermute('', x);