Lesson 1 · Unit 05
Strings
Cut, clean, search and rebuild text
What you practise
- Strings never change in place,
split,join,trim,replace,includes,slice,padStart, upper and lower case
Try a question from this lesson
Pick one
The second line asks for an upper-case version of the name. What does the third line print?
let name = "ada";
name.toUpperCase();
console.log(name);Correct answer
ada
Why
toUpperCase does not change the string. It builds a new one, "ADA", and hands it back. Line 2 never stores that result, so it is lost and name is still "ada".
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
Strings never change
A string has methods: functions you call with a dot, like push on an array.
There is one big difference. An array method like push changes the array. A string method never changes the string. It returns a new string, and you keep it by storing it.
Also new here: console.log takes several values, separated by commas, and prints them on one line with a space between.
let name = "ada";
name = name.toUpperCase(); // keep it
const low = "Hi".toLowerCase();
console.log(name); // ADA
console.log(name, low); // ADA hiConcept
split and join
split turns one string into an array of pieces. You say where to cut.
join is the way back: it glues the items of an array into one string. You say what goes between them.
Split, change the pieces, join: a great deal of text work is those three steps.
const words = "to be or not".split(" ");
console.log(words.length); // 4
const glued = words.join("-");
console.log(glued); // to-be-or-not
const letters = "abc".split("");
console.log(letters.length); // 310 more exercises in the app
- 3× type the answer
- 2× multiple choice
- 2× fill the blanks
- 1× true or false
- 1× match the pairs
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.