JavaScript Code

How to split string at new lines?


Problem Statement

given a string, split the string with new line character as delimiter

Solution

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

str.split('\n')

Example

1. split string at new lines

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