Sometimes, we want to split a comma separated string and process in a loop using JavaScript.
In this article, we’ll look at how to split a comma separated string and process in a loop using JavaScript.
How to split a comma separated string and process in a loop using JavaScript?
To split a comma separated string and process in a loop using JavaScript, we use the split
method and a for-of loop.
For instance, we write
const str = "Hello, World, etc";
const myArray = str.split(",");
for (const a of myArray) {
console.log(a);
}
to call str.split
with ','
to return an array with the strings between the commas.
And then we use a for-of loop to loop through the myArray
entries.
We get the entry being looped through from a
.
Conclusion
To split a comma separated string and process in a loop using JavaScript, we use the split
method and a for-of loop.