How to split a string in JavaScript

If you want to split a string according to a certain character or separator, you can use the split() method of JavaScript. The following example will show you how to split a string for each empty space. The returned value will be an array containing the split values.

Example : How to split a string in JavaScript
var str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
var tab = str.split(" ");    

// Display table values
for(var i = 0; i < tab.length; i++){
  document.write(" " + tab[i] + " ");



If you want to split the string by comma, just replace split(" ") by split(",").

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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