String Methods
Master every essential JavaScript string method! A complete reference with brief explanations and live examples for case, search, extract, transform, and modern string operations.
Master every essential JavaScript string method! A complete reference with brief explanations and live examples for case, search, extract, transform, and modern string operations. This hands-on tutorial focuses on practical implementation of string methods concepts.
String Methods π§
JavaScript strings come with a rich set of built-in methods. This lesson is your complete reference β every method is explained briefly so you know exactly when to reach for it.
π‘ Remember: Strings are immutable β methods always return a new string.
1. Case Methods π‘
These methods change the letter casing of a string.
| Method | What it does |
|---|---|
| toUpperCase() | Converts every character to uppercase |
| toLowerCase() | Converts every character to lowercase |
| toLocaleUpperCase(locale?) | Uppercase using locale-specific rules (e.g. Turkish Δ°) |
| toLocaleLowerCase(locale?) | Lowercase using locale-specific rules |
2. Whitespace Methods βοΈ
These methods remove extra spaces β very useful when cleaning user input from forms.
| Method | What it does |
|---|---|
| trim() | Removes whitespace from both ends |
| trimStart() | Removes whitespace from the start only |
| trimEnd() | Removes whitespace from the end only |
3. Search & Check Methods π
These methods let you check if something exists in a string, and where it is.
| Method | What it does |
|---|---|
| includes(str, pos?) | Returns true if string contains str |
| startsWith(str, pos?) | Returns true if string starts with str |
| endsWith(str, len?) | Returns true if string ends with str |
| indexOf(str, pos?) | Returns index of first match, or -1 |
| lastIndexOf(str, pos?) | Returns index of last match, or -1 |
| search(regex) | Returns index of first regex match, or -1 |
| match(regex) | Returns array of matches (or null) |
| matchAll(regex) | Returns iterator of all regex matches with capture groups |
4. Extraction Methods βοΈ
These methods extract a portion of a string without changing the original.
| Method | What it does |
|---|---|
| slice(start, end?) | Extracts from start to end (exclusive). Supports negative indices |
| substring(start, end?) | Like slice but ignores negative indices |
| at(index) | Returns character at index. Supports negative index (counts from end) |
| charAt(index) | Returns character at index (empty string if out of range) |
| charCodeAt(index) | Returns UTF-16 code of character at index |
| String.fromCharCode(...codes) | Creates a string from char codes |
5. Transform Methods π
These methods create modified copies of a string β the original is never changed.
| Method | What it does |
|---|---|
| replace(target, replacement) | Replaces the first occurrence |
| replaceAll(target, replacement) | Replaces all occurrences |
| split(separator, limit?) | Splits string into an array by separator |
| repeat(count) | Returns the string repeated count times |
| padStart(length, pad?) | Pads the start until length is reached |
| padEnd(length, pad?) | Pads the end until length is reached |
| concat(...strings) | Joins strings together (prefer template literals instead) |
6. Utility Methods π§ͺ
A few extra methods useful in specific situations.
| Method | What it does |
|---|---|
| length | Property β total number of characters in the string |
| localeCompare(str) | Compares strings in locale-aware order (returns -1, 0, or 1) |
| normalize(form?) | Normalises Unicode to a standard form (NFC, NFD, etc.) |
| codePointAt(index) | Like charCodeAt but handles full Unicode code points |
Practical Example: Name Formatter π€
AI Mentor
Confused about "JavaScript string methods: case, whitespace, search, extract, transform and utility"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 5Which method removes whitespace only from the beginning of a string?
Key Takeaways
β
Case: toUpperCase, toLowerCase β normalise and compare strings.
β
Whitespace: trim, trimStart, trimEnd β clean up user input.
β
Search: includes, startsWith, endsWith, indexOf, matchAll β find what you need.
β
Extract: slice, at, charAt β get substrings with ease.
β
Transform: replace, replaceAll, split, padStart, repeat β reshape strings.
What's Next?
Now that you're fluent with strings, let's make your code smart with Conditionals!
Keep coding! π