/

How to Split a String into Words in JavaScript

How to Split a String into Words in JavaScript

In JavaScript, there is a simple way to split a string into separate words using the split() method. With the split() method, we can define the delimiter, which determines where the string should be divided.

Here’s an example of how to use the split() method to cut a string into words when a space is encountered:

1
2
const text = "Hello World! Hey, hello!";
text.split(" ");

The split() method returns an array, and in this case, it will produce an array with four items:

1
[ 'Hello', 'World!', 'Hey,', 'hello!' ]

By utilizing the split() method, we can easily break a string into individual words using a specified delimiter.

tags: [“JavaScript”, “string manipulation”, “split string”, “array”]