Sub topic 8
Splitting Strings and Joining Arrays
This chapter assumes you know basic JavaScript syntax and array methods such as map and filter, and that you have read the earlier chapters on trim and slice. Two methods let you move between strings and arrays. split breaks a string into an array of pieces, and join puts an array back together as a string. Neither method changes its input. Both return new values.
The working model
str.split(separator, limit) searches str for separator and returns the text between the matches as an array of strings. The separator is not included in the result. The optional limit caps how many pieces you get back.
arr.join(separator) converts each element to a string and puts separator between them. If you leave out the separator, join uses a comma.
The pattern in the diagram is the most common use of these two methods. You split the string, clean each piece with an array method, and join the result:
const csv = " alice, bob ,carol ";
const names = csv.split(",").map((n) => n.trim());
console.log(names); // ["alice", "bob", "carol"]
console.log(names.join(" | ")); // alice | bob | carol
// The separator can also be a regular expression
console.log(csv.trim().split(/\s*,\s*/)); // ["alice", "bob", "carol"]In the regex version, trim still has to run first. The pattern \s*,\s* only removes the spaces next to commas, so without trim the first and last names would keep their outer spaces.
Common errors and edge cases
Most bugs with split come from empty strings in the result. The method keeps whatever text sits between matches, even when that text is empty.
console.log("".split(",")); // [""] (one empty string, not [])
console.log("a,b,".split(",")); // ["a", "b", ""]
console.log("a,,b".split(",")); // ["a", "", "b"]
console.log("a,,b,".split(",").filter(Boolean)); // ["a", "b"]
console.log("a,b,c".split(",", 2)); // ["a", "b"]
console.log("abc".split("")); // ["a", "b", "c"]
console.log("abc".split()); // ["abc"]
console.log([1, null, undefined, 4].join("-")); // 1---4
console.log([1, [2, 3]].join("-")); // 1-2,3
console.log([].join("-")); // "" (empty string)The behaviours to remember:
| Situation | Result | Why it matters |
|---|---|---|
| Splitting an empty string | `[""]` | Code like `if (parts.length)` treats empty input as one item |
| Trailing separator | Empty last element | You often need `filter(Boolean)` |
| No separator argument | The whole string in a one-element array | This is easy to confuse with `split("")` |
| `null` or `undefined` in `join` | An empty string for that element | No `"null"` text shows up |
| Nested arrays in `join` | The inner array is joined with commas | Call `flat()` first if you want a different separator |
Exercise
What does "2026-11-25".split("-").reverse().join("/") return, and does it change the original string?
Show answer
It returns "25/11/2026". The original string is unchanged: split creates a new array, reverse changes only that array, and join builds a new string.
Exercise
A user types "red, green,, blue," into a tags field. Write one expression that produces ["red", "green", "blue"].
Show answer
"red, green,, blue,".split(",").map((t) => t.trim()).filter(Boolean). The split call gives ["red", " green", "", " blue", ""], trim removes the spaces, and filter(Boolean) drops the empty strings.