Lesson 19 of 48
40%
String Methods
String methods are ready made helpers for text.
They can search, cut, change case, and clean spaces.
Think of a craft kit for words.
1
Change case
2
Trim spaces
3
Find text
4
Cut pieces
5
Replace and split
toUpperCase and toLowerCase
These make all letters big or small.
javascript
trim
trim() removes extra spaces from the ends.
Great for cleaning form answers.
javascript
includes and indexOf
includes asks if the text is there.
indexOf tells where it starts, or -1 if missing.
javascript
slice
slice cuts out a piece using start and end indexes.
The end index is not included.
javascript
replace
replace swaps the first match it finds.
javascript
split and join ideas
split breaks a string into an array.
Then you can work with each piece.
javascript
Tip: Methods return new text. Save the result if you want to keep it.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.