JavaScript Code

How to split string by new line in JavaScript?

Split string by new line in JavaScript

In this tutorial, you are given a string, and learn how to split the string with new line character as delimiter.


To split string str at new lines in JavaScript, call split() function on the string, and pass new line character as argument to split() function.



1. In the following program, we take string value in name variable, and split this string at new lines.

let name = 'apple\nbanana\ncherry';
let output = name.split('\n');