In JavaScript, you can use the split() method to split a string into an array of substrings. The split() method takes one argument, which is the delimiter that separates the substrings.

For example, to split a string of words separated by spaces:

var sentence = "The quick brown fox";
var words = sentence.split(" ");
console.log(words); // ["The", "quick", "brown", "fox"]

You can also split a string using a specific character, such as a comma:

var list = "apple,banana,cherry";
var items = list.split(",");
console.log(items); // ["apple", "banana", "cherry"]

You can also split a string into an array of substrings of a given length. You can use the .match() method with the .replace() method to achieve this.

var str = "Thequickbrownfox";
var chunks = str.match(/.{1,3}/g);
console.log(chunks); // ["The", "qui", "c", "kb", "row", "nfo", "x"]

It’s worth mentioning that the split() method will not change the original string, it will return a new array of substrings.

You can use any of the above methods to split a string in JavaScript, depending on your specific requirements.

Also Read:

Categorized in: