Sometimes, while working with javascript arrays, there is a requirement to convert an array to a string with every array element in a newline. This article demonstrates how to convert an array to a string with newlines embedded in the javascript string.

Convert array to string with newlines using join()

When applied to the calling array, javascript’s join(separator) method will join all the array elements into a single string divided by the separator passed in the method’s argument.

Example:-

Convert the array [“Javascript”, “Is”, “Popular”,”Language”] to string with every element of the array in a newline

Code:-

let stringArray = ["Javascript", "Is", "Popular","Language"];
let finalString  = stringArray.join('\r\n');
console.log(finalString);

Output:-

Javascript
Is
Popular
Language

Explanation:-

  • The join(‘\r\n’) method is used.
  • \r\n’ is passed in as the method’s argument that acts as a separator while converting the array to string values. The array elements are divided into newline using ‘\r\n’.
  • This string value is stored in finalString and printed on the console.

Read More:

I hope this javascript article helped you with conversions of the array to a string with all array elements into newline. Good Luck !!!