How to use the split() method In Javascript
Using split()
method with a chosen separator such as a space (‘ ‘). It offers a simple and versatile way to break a string into an array of distinct elements. The split() method is commonly used to split the string into an array of substrings by using specific separators.
Example: The below code Splits a string into an array of words using the split() method.
const string = `w3wiki is a leading platform that
provides computer science resources`;
const arrayofString = string.split(" ");
console.log(arrayofString);
Output
[ 'w3wiki', 'is', 'a', 'leading', 'platform', 'that', '\nprovides', 'computer', 'science', 'resources' ]
Split a String into an Array of Words in JavaScript
JavaScript allows us to split the string into an array of words using string manipulation techniques, such as using regular expressions or the split
method. This results in an array where each element represents a word from the original string. This can be achieved by using the following methods.
Table of Content
- Using the split() method
- Using match() method with regular expression
- Using spread operator with regex and match()
- Using a For Loop
- Using the reduce() Method