How to Split a String by Commas and Loop Through It Using JavaScript

Updated 2 weeks ago admin Javascript

In this lesson, we will see how to split a string by commas and loop through it using JavaScript. Let's assume we have a string of tags separated by commas and want to loop through it and display each tag separately.

Use the split method

To achieve that, we will use the split method, then loop through each tag and console.log it. 

const tags = "php, javascript, java";

tags.split(',').forEach(tag => {
  console.log(tag);
});