Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.split

Splits a string into an array by a separator.

Available since: ES3

Syntax

str.split(separator, limit)

Parameters

ParameterTypeDescription
separatorstringThe separator to split by.
limitnumberOptional. The maximum number of elements in the result.

Return value

ArrayAn array of the string's parts.

Examples

Splitting by a comma:

console.log('red,green,blue'.split(','));

Output:

[
  "red",
  "green",
  "blue"
]

An empty separator splits the string into characters:

console.log('code'.split('').join(' - '));

Output:

c - o - d - e

Notes

join() does the reverse. Strings with emoji are split into characters more reliably with [...str].

See also

Related recipes

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.