JavaScript Code

JavaScript String split()


String split()

str.split(sep)
  • str is original string
  • sep is a string (separator)

split() method split the string str using separator string sep, and returns an array containing the split parts.

Examples

1. split string using delimiter

let str = 'apple-banana-cherry';
let output = str.split('-');
console.log(output);

2. split string by single white space

let str = 'apple banana cherry';
let output = str.split(' ');
console.log(output);

3. empty separator to split() method. returns original string.

let str = 'apple banana cherry';
let output = str.split();
console.log(output);


Copyright @2022