Strings to Arrays and Back
A lot of text has parts inside it: a line from a CSV file, a date with dashes, a sentence made of words. split cuts a string into those parts and gives you an array:
console.log("a,b,c".split(","));
console.log("hello world".split(" "));
console.log("2026-10-31".split("-"));
Output:
[ 'a', 'b', 'c' ]
[ 'hello', 'world' ]
[ '2026', '10', '31' ]
split cuts the string at every place where the separator appears. The separator itself is removed. You have used this since the input lessons: .split("\n") cuts the input into lines, and .split(" ") cuts a line into words.
If you split on an empty string "", you get every character as its own element:
console.log("cat".split(""));
Output:
[ 'c', 'a', 't' ]
The parts are always strings, even when they look like numbers. Convert a part before you do math with it:
const parts = "Emma,28,Paris".split(",");
console.log(parts[1] + 1);
console.log(Number(parts[1]) + 1);
Output:
281
29
The first line joins "28" and 1 as text. The second line converts first, so it really adds.
join goes the other way. You met it in the Array Methods section. It glues the elements of an array into one string, with your separator between them:
const parts = ["a", "b", "c"];
console.log(parts.join(" | "));
Output:
a | b | c
Split with one separator and join with another, and you have changed how the data is written. Commas in, pipes out.
The main idea of this lesson is that split turns a text problem into an array problem. After the split, you can use every array tool you know: map, filter, toSorted, reduce. Then join turns the result back into text. For example, to reverse a word, split it into characters, reverse the array and join it again:
const word = "stressed";
console.log(word.split("").toReversed().join(""));
Output:
desserts
In the exercise, split a name,age,city line, use its parts, and join them again with " | ".