Home >>JavaScript String Methods >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 | 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. |
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>
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>
Click the button to see the Output.