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
