Skip to content

JavaScript split a sentence into an array of words | Example code

  • by

Just use the split() method to convert sentences into an array of words in JavaScript. You have to use a separator which uses to indicate where each split should occur. We will use a space separator, you can use a comma or another.

JavaScript split a sentence into an array of words Example

HTML example code of split a string into an array of substrings, and returns the new array.

<!DOCTYPE HTML> 
<html> 

<body> 

	<script>
		var str = "Hello world, Simple example";
		
		var splitStr = str.split(" ");

		console.log(splitStr);
	</script> 
</body> 
</html>	

Output:

JavaScript split a sentence into an array of words

Do comment if you have any doubts and suggestions on this JS array topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *