.join() and .split()

The full text of this chapter, to read or revisit. To practise, do the exercises.

▶ Do the exercises

Exercise 1 — Turning a List Into a Sentence

Imagine you want to display your active Codémon team on the game screen as a clean text banner, like `"Aquachu - Pyrochu - Zap"`, instead of a raw JavaScript array.

The `.join()` method combines all the elements of an array into a single string, placing the separator of your choice between each item:

const team = ["Aquachu", "Pyrochu", "Zap"]; console.log(team.join(", ")); console.log(team.join(" - ")); console.log(team.join(" and "));

Notice that the separator is only placed between elements, never at the very beginning or the very end. With 3 Codémon in the array, the separator appears 2 times.

Exercise 2 — The Default and the Empty Case

Two special cases are very handy to know with `.join()`: - Passing an empty string `""` glues all the elements directly together with no characters in between (great for combining individual letters into a word). - Passing no argument at all defaults to separating items with a comma `","` (with no spaces).

const letters = ["Z", "a", "p"]; console.log(letters.join("")); console.log(letters.join());

Just like `.slice()`, `.join()` never modifies the original array—it simply returns a new string. It also works on arrays of numbers, automatically converting each number into text:

const levels = [12, 8, 4]; console.log("Levels: " + levels.join(" / ")); console.log(levels);

Exercise 3 — Quiz: Joined

What does this code print?

const parts = ["a", "b", "c"]; console.log(parts.join("-"));
Options
• a-b-c
• -a-b-c-
• abc
• a,b,c

Hint : The separator sits between the elements only.

Exercise 4 — A Readable Team

Print the team names separated by a comma and a space.

Hint : One method builds a string, and the separator must include the space you want to see.

Exercise 5 — Cutting a String Into an Array

`split` is the exact opposite: it takes a string and cuts it wherever it finds the separator, returning an array:

const line = "Aquachu,Pyrochu,Zap"; const team = line.split(","); console.log(team.length, team);

This is how you read data that arrives as a single line of text: a save file, a line typed by a player, a row from a spreadsheet.

Splitting on an empty separator cuts between every character:

const name = "Zap"; console.log(name.split("")); console.log(name.split("").length);

Exercise 6 — Quiz: How Many Pieces?

What does this code print?

const line = "a-b-c-d"; console.log(line.split("-").length);
Options
• 4
• 3
• 7
• 1

Hint : Count the pieces the cuts produce, not the cuts.

Exercise 7 — Combining split and join

Strings don't have array methods like `.map()` or `.filter()`. So when you need to transform each word inside a string, you can combine `.split()` and `.join()` in three simple steps:

1. `.split(",")` turns `"aquachu,pyrochu,zap"` into the array `["aquachu", "pyrochu", "zap"]`. 2. `.map(...)` transforms each name to uppercase: `["AQUACHU", "PYROCHU", "ZAP"]`. 3. `.join(" | ")` glues the array back into a clean string: `"AQUACHU | PYROCHU | ZAP"`.

const line = "aquachu,pyrochu,zap"; const banner = line .split(",") .map((name) => name.toUpperCase()) .join(" | "); console.log(banner);

Exercise 8 — Split Then Count

Count how many names the line contains.

Hint : One method cuts the text into a list, then the list is measured as usual.

Exercise 9 — join vs split

Match each element with its description.

Items to match
• arr.join("-")
• str.split("-")
• [1,2].join("")
• "ab".split("")
Definitions
• An array becomes a string
• A string becomes an array
• "12"
• An array of 2 characters

Exercise 10 — Save File Reader

A save file stores the team as a single line of text. Read it, clean it up and display it.

Step 1: cut `saveLine` into an array on the `;` separator, and print `"Members: N"`. Step 2: print each name on its own line with `"- "` in front. Step 3: build `display`, the names joined with `" / "`, and print it. Step 4: build `shout`, the names in uppercase joined with `" + "`, and print it.

Expected output:

Members: 3 - Aquachu - Pyrochu - Zap Aquachu / Pyrochu / Zap AQUACHU + PYROCHU + ZAP
Starter code
const saveLine = "Aquachu;Pyrochu;Zap"; // Write your code here
Back to the course