/

HTML / CSS / JavaScript Tutorial

JavaScript String method: split()

[this page | pdf | back links]

The split() method (when applied to a JavaScript string) returns an array of substrings that are created by splitting the original string using a given delimiter.

 

It has the following syntax with the following parameters:

 

string.split(delimiter,limit)

 

Parameter

Required / Optional

Description

delimiter

Optional

Separator used to delimit individual entries. If the delimiter is "" (i.e. an empty string) then the string is split between each character. If the delimiter is not present then split does not affect the original string

limit

Optional

An integer specifying maximum number of splits (items after the limit will not be included in output array)

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head>
<style>
table,th,tr,td {border: 1px solid black; border-collapse: collapse;}
</style>
</head>
<body>
<table>
<tr>
<th>Example</th>
<th>Resulting value of <code>x</code></th>
</tr>
<tr>
<td><code id="Example"></code></td>
<td><code id="Result"></code></td>
</tr>
</table>

<script>
var list = "a,b,c";
document.getElementById("Example").innerHTML =
  'var list = "' + list + '";<br>' +
  'var y = list.split(",");<br>' +
  'var x = y[1];';
document.getElementById("Result").innerHTML
  = list.split(",")[1];
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodStringSplit() {
  var z = "abc"; return !!z.split;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript String Variables


Desktop view | Switch to Mobile