split method

The split method splits string into an array according to the separator (delimiter) specified in the first parameter. If it is not set, the entire string will be returned. If it is given as empty quotes, then each character of the string will go into a separate element of the array. As the second optional parameter, you can specify the maximum number of elements in the resulting array.

Syntax

javascriptstring.split([separator], [maximum number of elements]);

Example

Let some string with hyphens be given. Let's split this string into an array by the separator '-':

javascriptlet str = 'ab-cd-ef';
let arr = str.split('-');

console.log(arr);

The code execution result:

javascript['ab', 'cd', 'ef']

Example

Let's again split a string by the delimiter '-', but specify the maximum number of elements in the resulting array as the second parameter, for example, 2. In this case, only 2 elements will be written to the array:

javascriptlet str = 'ab-cd-ef';
let arr = str.split('-', 2);

console.log(arr);

The code execution result:

javascript['ab', 'cd']

Example

Let's write each character of a string into a separate element of the array:

javascriptlet str = 'abcde';
let arr = str.split('');

console.log(arr);

The code execution result:

javascript['a', 'b', 'c', 'd', 'e']

Example

Let's write the first 3 characters of a string into a separate elements of an array:

javascriptlet str = 'abcde';
let arr = str.split('', 3);

console.log(arr);

The code execution result:

javascript['a', 'b', 'c']

Example

Let's use split to split a string of digits. Note that the result will be an array of strings, not an array of numbers:

javascriptlet str = '12345';
let arr = str.split('');

console.log(arr);

The code execution result:

javascript['1', '2', '3', '4', '5']

Example . Usage

Let's reverse the characters of a string. To do this, split the string into an array using split with the separator '' (this will place each character of the string in a separate element of the array), reverse this array using reverse and then join the reversed array back with join:

javascriptlet str = '123456789';
let arr1 = str.split('');
let arr2 = arr1.reverse();
let res = arr2.join('');

console.log(res);

The code execution result:

javascript'987654321'

Example . Usage

Let's simplify the solution of the previous problem - let's merge all the commands into single chain:

javascriptlet str = '123456789';
let res = str.split('').reverse().join('');

console.log(res);

The code execution result:

javascript'987654321'

Example . Usage

Given a string with digits. Let's find the sum of the digits from this string. To do this, let's split the string into an array, and then iterate through this array and find its sum. We have a catch: split returns strings, so when summing, we convert these string digits into real digits using Number:

javascriptlet str = '12345';
let arr = str.split('');
let sum = 0;

for (let i = 0; i < arr.length; i++) {
  sum += Number(arr[i]);
}

console.log(sum);

The code execution result:

javascript15

Example . Usage

Given a number. Let's write each digit of this number into a separate element of the array. There is a catch here - split only applies to strings, and we have a number. We convert number to string first with String and then apply split:

javascriptlet num = 12345;
let str = String(num);
let arr = str.split('');

console.log(arr);

The code execution result:

javascript['1', '2', '3', '4', '5']

See also

  • the join method
    that merges array elements into a string
  • the split method
    that splits a string by regex
  • the reverse method
    that reverses the order of elements in an array
  • the length property
    that allows you to find the length of a string