How to Split a String by Comma and Loop Through Using JavaScript

1 year ago admin Javascript

In this lesson, we will see how to split a string by comma and loop through using javascript, so let's assume that we have a string of tags separated by a comma and we want to loop through the string and display each tag separately.


Use the split method

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

                                                        
                                                                                                                        
const tags = "php, javascript, java";

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

Popular Tutorials

Related Tutorials

How to Get the Index of a Value in an Array in JavaScript

In this lesson, we will see how to get the index of a value in an array in JavaScript, sometimes we...


How to Join Elements of an Array in JavaScript

In this lesson, we will see how to join elements of an array in JavaScript, sometimes we want to joi...


How to Check if an Array Includes a Value in JavaScript

In this lesson, we will see how to check if an array includes a value in JavaScript, sometimes we wa...


How to Remove a Specific Value from an Array in JavaScript

In this lesson, we will see how to remove a specific value from an array in JavaScript, sometimes we...


How to Add Value to an Existing Array in JavaScript

In this lesson, we will see how to add value to an existing array in JavaScript, sometimes we want t...


How to Remove Whitespace from a String in JavaScript

In this lesson, we will see how to remove whitespace from a string in JavaScript, sometimes we want&...


How to Remove Spaces from a String in JavaScript

In this lesson, we will see how to remove spaces from a string in JavaScript, sometimes we want to r...


How to Check if a String Starts with a Character in JavaScript

In this lesson, we will see how to check if a string starts with a desired character in JavaScript,...


How to Check if a String Contains a Substring in JavaScript

In this lesson, we will see how to check if a string contains a substring in JavaScript, sometimes w...


How Do you Check if a String ends with a Character in JavaScript

In this lesson, we will see how to check if a string ends with a desired character in JavaScript, so...