JavaScript String split()

Examples

Split the words:

let text = "How are you doing today?";
const myArray = text.split(" ");
Try it Yourself »

Split the words, and return the second word:

let text = "How are you doing today?";
const myArray = text.split(" ");
let word = myArray[1];
Try it Yourself »

Split the characters, including spaces:

const myArray = text.split("");
Try it Yourself »

Use the limit parameter:

const myArray = text.split(" ", 3);
Try it Yourself »

More examples below.


Description

The split() method splits a string into an array of substrings.

The split() method returns the new array.

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

If (" ") is used as separator, the string is split between words.


Syntax

string.split(separator, limit)

Parameters

Parameter Description
separator Optional.
A string or regular expression to use for splitting.
If omitted, an array with the original string is returned.
limit Optional.
An integer that limits the number of splits.
Items after the limit are excluded.

Return Value

Type Description
ArrayAn array containing the splitted values.


More Examples

Split a string into characters and return the second character:

const myArray = text.split("");
Try it Yourself »

Use a letter as a separator:

const myArray = text.split("o");
Try it Yourself »

If the separator parameter is omitted, an array with the original string is returned:

const myArray = text.split();
Try it Yourself »

Browser Support

split() is an ECMAScript1 (ES1) feature.

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.