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 How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one 2 min read How to Separate Array of Objects into Multiple Objects in JavaScript ? In JavaScript, the task of separating an array of objects into multiple objects involves organizing and restructuring data based on specific criteria or properties. This process is often necessary when dealing with datasets that need to be grouped or segmented for easier analysis or manipulation. Th 5 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 Create a string with multiple spaces in JavaScript We have a string with extra spaces and if we want to display it in the browser then extra spaces will not be displayed. Adding the number of spaces to the string can be done in the following ways. In this article, we are going to learn how to Create a string with multiple spaces in JavaScript. Below 3 min read How to convert array of strings to array of numbers in JavaScript ? Converting an array of strings to an array of numbers in JavaScript involves transforming each string element into a numerical value. This process ensures that string representations of numbers become usable for mathematical operations, improving data handling and computational accuracy in applicati 4 min read How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app 5 min read Like