String split() method in JavaScript

You can use the split() method in JavaScript to convert a string into an array. The split() methods splits a string by separating it into substrings. The split() method returns an array of strings split at every point where separator occurs.


str.split([separator], [limit]);

Parameters Details:

separator: separator specifies a character that is used to operate the string.
separator parameter is optional. If a separator is not passed then an array of one element containing the whole string is returned.

limit: limit specifies the limit on the number of splits to be found.


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial- 
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>split method</title>
      var str = "JavaScript is awesome.";
      var splittedString = str.split(' ', 3);
      document.write("Splitted string is: ", splittedString);


Splitted string is: JavaScript,is,awesome.

Leave a Reply

Your email address will not be published. Required fields are marked *