How to split multiline string into an array of lines in JavaScript ? Last Updated : 05 Jan, 2023 Comments Improve Suggest changes Like Article Like Report Multiline string in JavaScript means a string having two or more lines. To split a multiline string into an array we need to use split() in our JavaScript code. JavaScript split(separator, limit) Method: The split() function is used to split the data depending upon the attributes we are passing in it. The separator attributes specify that from this word/sign the string will be divided. The limit attribute is optional, it specifies how many splits will be there. Example 1: This example shows the use of the above approach. In this, when we will click on the "Go" button the array of the multiline string will be displayed on the screen separated by ",". HTML <h1 style="color:green"> Welcome to Geeks for Geeks </h1> <h3> Split multiline string into an array of lines in JavaScript </h3> <button onclick="myFunction()"> Go </button> <p id="StringToArray"></p> <script> function myFunction() { var string = "Are you ready?" + "<br>So let's get started"; var array = string.split("<br>"); document.getElementById("StringToArray") .innerHTML = array; } </script> Output: Split multiline string into an array of lines Example 2: Now, let's see how to get a particular index of an array. HTML <h1 style="color:green"> Welcome to Geeks for Geeks </h1> <h3> Split multiline string into an array of lines in JavaScript </h3> <button onclick="myFunction()">Go</button> <p id="StringToArray"></p> <script> function myFunction() { var string = "Are you ready?" + "<br>So let's get started"; var array = string.split("<br>"); document.getElementById("StringToArray") .innerHTML = array[1]; } </script> Output: Split multiline string into an array of lines As we wrote array[1], hence only the 2nd line has been printed. If we will write array[2], then it will be undefined as this array contains data in the first two indexes only that are 0 and 1 respectively. Example 3: Now, let's try to take a string as user input. HTML <h1 style="color:green"> Welcome to Geeks for Geeks </h1> <h3> split multiline string into an array of lines in JavaScript </h3> <textarea id="write" placeholder="Write something" style="height:100px;"> </textarea> <button onclick="myFunction()">Go</button> <p id="StringToArray"></p> <script> function myFunction() { var string = document .getElementById("write").value; var array = string.split("."); document.getElementById("StringToArray") .innerHTML = array; } </script> Output: Split multiline string into an array of lines Comment More infoAdvertise with us Next Article How to split multiline string into an array of lines in JavaScript ? G gaursheetal322 Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Methods JavaScript-Questions +2 More Similar Reads Split a String into an Array of Words in JavaScript JavaScript allows us to split the string into an array of words using string manipulation techniques, such as using regular expressions or the split method. This results in an array where each element represents a word from the original string. 1. Using split() MethodUsing split() method with a chos 5 min read How To Split a String Into Segments Of n Characters in JavaScript? When working with JavaScript, you might encounter a situation where you need to break a string into smaller segments of equal lengths. This could be useful when formatting a serial number, creating chunks of data for processing, or splitting a long string into manageable pieces. In this article, we 6 min read How to create multi-line strings in JavaScript ? In JavaScript, the absence of native support for multi-line strings was a longstanding challenge. However, with the advent of ES6 (ECMAScript 2015), the things changed dramatically. ES6 introduced string literals, offering robust support for multi-line strings, which was a significant enhancement fo 3 min read Split an Array into Chunks in JavaScript Here are different methods to split an array into chunks in JavaScript.1. Using slice() MethodThe array slice() method returns a new array containing the selected elements. This method selects the elements starting from the given start argument and ends at, but excluding the given end argument. Synt 3 min read How to Iterate Over Characters of a String in JavaScript ? There are several methods to iterate over characters of a string in JavaScript. 1. Using for LoopThe classic for loop is one of the most common ways to iterate over a string. Here, we loop through the string by indexing each character based on the string's length.Syntaxfor (statement 1 ; statement 2 2 min read Like