What are Strings?
Strings are sequences of characters used to represent text. In JavaScript, strings are immutable β once created, they cannot be changed, but you can create new strings from them.
π‘ Key insight
String methods don't modify the original string β they return a new string. This is because strings are immutable in JavaScript.
Common String Properties and Methods
lengthβ get the length of a stringtoUpperCase()β convert to uppercasetoLowerCase()β convert to lowercasetrim()β remove whitespace from both endsslice(start, end)β extract a portionsplit(separator)β split into an arrayreplace(old, new)β replace textincludes(substring)β check if text existsstartsWith(substring)β check if string starts with textendsWith(substring)β check if string ends with text
Common Mistakes
- Forgetting that strings are immutable (methods return new strings)
- Using
split()without checking if the separator exists - Confusing
slice()withsplice()(which is for arrays)
Quick Quiz
1. Which method splits a string into an array?
2. Which method removes whitespace from both ends of a string?
3. What does "hello".toUpperCase() return?
Summary
- Strings are sequences of characters (text)
- Strings are immutable β methods return new strings
- Key methods:
length,toUpperCase(),toLowerCase(),trim(),slice(),split(),replace(),includes() join()combines array elements into a string- Template literals (
`${}`) allow string interpolation
