How to split a string into words in JavaScript

#javascript #string

Last updated on Sep 15, 2021 by Suraj Sharma



In this tutorial, you’ll learn how you can split a sentence into words in JavaScript using the split() method.

To divide a string into words you can use the string.split() method and pass a whitespace as an argument to the split() method. It returns an array of words.


Solution

const sentence = "Split a sentence into words in JavaScript";
sentence.split(" ");

// ["Split", "a", "sentence", "into", "words", "in", "JavaScript"]


Related Solutions


Rate this post


Suraj Sharma is a Full Stack Software Engineer. He holds a B.Tech degree in Computer Science & Engineering from NIT Rourkela.