javascript · 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.
A is a trick a value knows how to do. Write the box name, a dot, then the method name and brackets. pet.toUpperCase() shouts in capitals and pet.toLowerCase() whispers in small letters.
.trim() cuts spaces off both ends, handy when someone types an extra space. .replace("B", "W") swaps the first B it finds for a W. Each method hands back a new string.
A is a fact a value knows about itself, so it needs no brackets. pet.length counts every letter, space and symbol in the string.
let pet = "Biscuit";
console.log(pet.toUpperCase());
console.log(pet.toLowerCase());
console.log(pet.replace("B", "W"));
console.log(pet.length); You can line tricks up, so pet.trim().toUpperCase() trims first, then shouts.
guess first
Some extra spaces. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
let reply = " yes ";
console.log(reply.length);
console.log(reply.trim().length);
your turn
Your turn. Shout about your favourite food.
Type your favourite food between the quotes. Then press run to see it in capitals and find out how long it is.
fix it
Counting letters crashed. Can you fix it?
- It is a TypeError: the string was asked to do a trick it does not know.
lengthis a property, not a method. Properties have no brackets.- Remove the () after length, so it reads fruit.length, then press run.
nailed it
your strings can do tricks now. :D
- A method is a trick with brackets, like
.trim(). - A property is a fact with no brackets, like
.length. - Methods hand back a new string and leave the box alone.
new words in your collection
- method
- property
This lesson isn’t saved. A free account remembers every lesson you finish.