<!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> </head> <body> <script> function combinator(matrix){ return matrix.reduceRight(function(combination, x){ var result = []; [...x].forEach(function(a){ [...combination].forEach(function(b){ result.push(a + b); }); }); return result; }); }; document.write( JSON.stringify(combinator( ["a", "bc", "def"])) ); </script> </body> </html>