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.

Syntax:

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.

Example:

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

Output:

Splitted string is: JavaScript,is,awesome.

Leave a Reply