Ad space (banner)
🟨JavaScript Lessons
Lesson 21 / 69

Splitting and Joining Strings (split and join)

In this lesson you'll learn how to split and join strings with JavaScript's .split() and .join(), so you can freely process CSV-like text data. This is for people searching "JavaScript split join usage."

.split(separator) splits a string into an array. Conversely, .join(separator) combines an array into a single string. These are frequently used when handling CSV-like data. Combining the two lets you freely process text data — converting separator characters, stripping extra spaces, and more.

The sample code splits the comma-separated string csv into the array fruits with .split(","), then reassembles that array into a string using a different separator (a slash) with .join(" / "). If you use an empty string as the separator, you can also break a string into an array of individual characters.

A common beginner stumbling block is that the separator you pass to .split() must exactly match the separator that actually appears in the original string, or the split won't come out correctly. Real-world CSV often contains inconsistent formatting — like extra whitespace after a comma — so splitting with a regular expression is sometimes necessary.

In real development, split/join combinations are a frequently used technique that shows up any time you're handling text data — reading a file, processing an API response, or parsing a comma-separated list of tags entered in a form.

You can also pass a regular expression to .split(), which lets you handle more complex patterns, like "split on a comma or a space." When dealing with real-world data that's often inconsistently formatted, it's worth knowing about this more flexible way to split.

If you only need to check whether a particular substring appears somewhere inside a string, there's also a way to do it without splitting at all, using .includes(). Get comfortable choosing between splitting for detailed processing and just checking whether something is contained, depending on what you need.

To turn a string into an array of individual characters, you use .split(""), but special characters like emoji sometimes don't split correctly this way — for more advanced string processing, a technique using Array.from() is also known.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)