.slice() and .substring()
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — Extracting a Piece of Text
Imagine you have a Codémon named `"Aquachu"` and you want to grab its first three letters to create a short tag (`"Aqu"`). Picking characters one index at a time works, but it quickly gets tedious:
const name = "Aquachu";
const nickname = name[0] + name[1] + name[2];
console.log(nickname);Instead, you can use the `.slice(start, end)` method to extract a section of a string in a single step: 1. The first number (`start`) is the index where extraction begins (included). 2. The second number (`end`) is the index where extraction stops (excluded).
const name = "Aquachu";
const nickname = name.slice(0, 3);
console.log(nickname);Because index `3` is excluded, `name.slice(0, 3)` grabs the characters at indexes `0`, `1`, and `2` (`"Aqu"`). Try changing `(0, 3)` to `(0, 4)` to see how it works!
Exercise 2 — The Original String Never Changes
In JavaScript, strings are immutable, which means an existing string can never be modified in place. Every string method—including `.slice()`—returns a brand-new string and leaves the original variable untouched:
const name = "Aquachu";
const shortName = name.slice(0, 3);
console.log(shortName);
console.log(name);Notice that `shortName` holds the extracted piece (`"Aqu"`), while `name` still holds `"Aquachu"`. Because `.slice()` does not change `name`, calling it without saving or using its return value has no effect at all:
const name = "Aquachu";
// The returned string is not stored anywhere!
name.slice(0, 3);
console.log(name);Exercise 3 — Shortcuts and Negative Indexes
Leave out the second argument and `.slice()` runs all the way to the end of the string:
const name = "Aquachu";
console.log(name.slice(4));
console.log(name.slice(1));A negative index counts backwards from the end. This is the easy way to grab the last few characters without knowing the length:
const name = "Aquachu";
console.log(name.slice(-3));
console.log(name.slice(-1));Exercise 4 — Quiz: Which Piece?
What does this code print?
const move = "Thunder";
console.log(move.slice(0, 4));ThunThundhundnderHint : The starting index joins the piece, the ending one marks where to stop.
Exercise 5 — Three-Letter Code
Build a three-letter code from the first three characters of the name.
Hint : One method cuts a piece of text, and the end position is not included in it.
Exercise 6 — substring
`.substring()` does almost the same job with the same two arguments, and for normal use the two are interchangeable:
const name = "Aquachu";
console.log(name.slice(0, 3));
console.log(name.substring(0, 3));The difference shows up with negative numbers. `.slice()` counts from the end, while `.substring()` treats any negative number as `0`:
const name = "Aquachu";
console.log(name.slice(-3));
console.log(name.substring(-3));Which one to use?
Prefer `.slice()`: it behaves the same way on strings and on arrays, and it supports negative indexes. Keep `.substring()` in mind only because you will meet it in other people's code.
Exercise 7 — Quiz: The Negative Difference
What does this code print?
const name = "Zapchu";
console.log(name.substring(-2));Zapchuhuchu""Hint : One of the two methods refuses negative numbers and starts from the beginning instead.
Exercise 8 — The Last Two Letters
Print the last two characters of the name, whatever its length.
Hint : Counting backwards from the end needs one specific sign in front of the number.
Exercise 9 — slice Cheat Sheet
`const s = "Aquachu";`. Match each expression with its result.
s.slice(0, 3)s.slice(4)s.slice(-1)s.substring(-3)"Aqu""chu""u""Aquachu"Exercise 10 — Nickname Generator
The game gives every Codémon a short nickname and a battle tag, built automatically from its full name.
Step 1: `nickname(name)` returns the first three characters of the name. Step 2: `tag(name)` returns the first three characters in uppercase, a dash, then the last two characters. For `"Aquachu"` it returns `"AQU-hu"`. Step 3: for each name in `roster`, print `"name -> nickname (tag)"`.
Expected output:
Aquachu -> Aqu (AQU-hu)
Pyrochu -> Pyr (PYR-hu)
Metachu -> Met (MET-hu)const roster = ["Aquachu", "Pyrochu", "Metachu"];
// Write your functions here
// Use them here