Search w3schools.com:

SHARE THIS PAGE

JavaScript split() Method

String Object Reference JavaScript String Object

Example

Split a string into an array of substrings:

var str="How are you doing today?";
var n=str.split(" ");

The result of n will be an array with the values:

How,are,you,doing,today?

Try it yourself »

Definition and Usage

The split() method is used to split a string into an array of substrings, and returns the new array.

Tip: If an empty string ("") is used as the separator, the string is split between each character.

Note: The split() method does not change the original string.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The split() method is supported in all major browsers.


Syntax

string.split(separator,limit)

Parameter Values

Parameter Description
separator Optional. Specifies the character, or the regular expression, to use for splitting the string. If omitted, the entire string will be returned (an array with only one item)
limit Optional. An integer that specifies the number of splits, items after the split limit will not be included in the array

Return Value

Type Description
Array An array containing the splitted values

Technical Details

JavaScript Version: 1.1


More Examples

Example

Omit the separator parameter:

var str="How are you doing today?";
var n=str.split();

The result of n will be an array with only one value:

How are you doing today?

Try it yourself »

Example

Separate each charater, including white-space:

var str="How are you doing today?";
var n=str.split("");

The result of n will be an array with the values:

H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?

Try it yourself »

Example

Use the limit parameter:

var str="How are you doing today?";
var n=str.split(" ",3);

The result of n will be an array with only 3 values:

How,are,you

Try it yourself »

Example

Use a letter as a separator:

var str="How are you doing today?";
var n=str.split("o");

The result of n will be an array with the values:

H,w are y,u d,ing t,day?

Try it yourself »


String Object Reference JavaScript String Object

W3Schools Certification

W3Schools' Online Certification

The perfect solution for professionals who need to balance work, family, and career building.

More than 10 000 certificates already issued!

Get Your Certificate »

The HTML Certificate documents your knowledge of HTML.

The HTML5 Certificate documents your knowledge of advanced HTML5.

The CSS Certificate documents your knowledge of advanced CSS.

The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.

The jQuery Certificate documents your knowledge of jQuery.

The XML Certificate documents your knowledge of XML, XML DOM and XSLT.

The ASP Certificate documents your knowledge of ASP, SQL, and ADO.

The PHP Certificate documents your knowledge of PHP and SQL (MySQL).

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]