We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Covert array to string with newlines embedded in string

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:-

Advertisements

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('rn');
console.log(finalString);

Output:-

Javascript
Is
Popular
Language

Explanation:-

  • The join(‘rn’) method is used.
  • rn’ 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 ‘rn’.
  • 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 !!!

Cart

Your Cart is Empty

Back To Shop