Converting multi-dimensional array to string in JavaScript

We are required to write a JavaScript function that takes in a nested array of literals and converts it to a string by concatenating all the values present in it to the string. Moreover, we should append a whitespace at the end of each string element while constructing the new string.

Let’s write the code for this function −


The code for this will be −

const arr = [
   'this', [
      'is', 'an', [
         'example', 'of', [
            'nested', 'array'
const arrayToString = (arr) => {
   let str = '';
   for(let i = 0; i < arr.length; i++){
         str += `${arrayToString(arr[i])} `;
         str += `${arr[i]} `;
   return str;


The output in the console −

this is an example of nested array

Updated on: 15-Oct-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started