How to split multiline string into an array of lines in JavaScript ? - GeeksforGeeks

13 Apr, 2021
Multiline string in JavaScript means a string having two or more lines. To split a multiline string to an array we need to use split() in our JavaScript code. 
split(separator, limit): 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:
HTML
<!DOCTYPE html><html>  <body>      <h1>Welcome to Geeks for Geeks</h1>      <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></body>  </html>
Output:
In this, when we will click on the “Go” button the array of the multiline string will be displayed on the screen separated by “,”.
Example 2: Now, let’s see how to get a particular index of an array.
HTML
<!DOCTYPE html><html>  <body>      <h1>Welcome to Geeks for Geeks</h1>      <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></body>  </html>
Output:
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 string as an user input.
HTML
<!DOCTYPE html><html>  <body>    <h1>Welcome to Geeks for Geeks</h1>      <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></body>  </html>
Output:
JavaScript-Methods
JavaScript-Questions
javascript-string
Picked
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to calculate the number of days between two dates in javascript?
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Convert a string to an integer in JavaScript
File uploading in React.js
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?