Home >>JavaScript String Methods >JavaScript String split() Method

JavaScript String split() Method

JavaScript String split() Method

JavaScript split() method is used to split a given input string into an array of substrings and returns the new splitted array as output. It does not change the original given string.

Syntax:

string.split(separator, limit)

Parameter Values

Parameter Description
separator This is an optional parameter. It defines the character, or the regular expression, to use for splitting the string.
limit This is an optional parameter. It defines an integer that specifies the number of splits.

Browser Support

Method Chrome Edge Firefox Safari Opera
split() Yes Yes Yes Yes Yes

Here is an example of split() method:

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "Hello everyone.. I am Jerry..";
  var res = str.split(" ");
  document.getElementById("demo").innerHTML = res;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

 

Example 2:

<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction1()">Try it</button>
<p id="demo1"></p>
<script>
function myFunction1() {
  var str = "Hello everyone.. Welcome to PHPTPOINT";
  var res = str.split("");
  document.getElementById("demo1").innerHTML = res;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.

 


No Sidebar ads