Skip to content

JavaScript split a string into an Array | Example code

  • by

Use the split() method to split a string into an Array. When specifying an empty string as the separator, the split() method will return an array with one element per word.

JavaScript split string into array Example code

HTML example code:

Split a string into an array of words or substring and returns the new array using the split() method. It doesn’t change the original string.

Splitting is based on string space.

<!DOCTYPE HTML> 
<html> 

<body> 

	<script>
		
		var str = "Hello world is basic Program?";
		var res = str.split(" ");
		
		console.log(res);

	</script>
</body> 
</html>		

Output:

JavaScript split a string into an Array

Do comment if you have any doubts and suggestions on this JS string and 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

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading