typescript · level 1 · unit 3 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
Strings come with tricks built in.
Every string carries tools with it. A is a tool you use with a dot and brackets. pet.toUpperCase() gives back the string in capitals, and pet.toLowerCase() in small letters.
.trim() cuts spaces off both ends, and .replace("B", "W") swaps the first B for a W. Each one gives back a new string. The box itself stays the same.
A is a fact about a value, read with a dot but no brackets. pet.length tells you how many letters, spaces and symbols the string has.
let pet: string = "Biscuit";
console.log(pet.toUpperCase());
console.log(pet.toLowerCase());
console.log(pet.replace("B", "W"));
console.log(pet.length);
console.log(" hi ".trim()); Methods need brackets, even empty ones. Properties never have them.
guess first
Help! Will this shout? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let shout: string = "help";
shout.toUpperCase();
console.log(shout);
your turn
Your turn. Tidy up a messy name.
Type your name between the spaces on line 1. In the second blank, type the method that cuts spaces off both ends, then press run.
fix it
How long is popcorn? This will not run.
- Read the error:
TS2349: This expression is not callable.Callable means you can use it with brackets. lengthis a property, a fact about the string. Properties never have brackets.- Remove the () after length, then press run.
nailed it
your strings know some tricks now. :D
- A method is a tool used with a dot and brackets.
- A property like
.lengthhas no brackets. - Methods give back a new string. Store it to keep it.
new words in your collection
- method
- property
This lesson isn’t saved. A free account remembers every lesson you finish.