String Methods
๐ What are JavaScript String Methods? JavaScript strings come packed with built-in methods for transforming, searching, and extracting text. Like Python, JS strings are immutable: methods always return a new string. Chain multiple methods together for powerful one-line transformations.

Appy Saysโฆ
Strings are everywhere in web apps โ usernames, messages, URLs, search terms. JavaScript's string methods let you slice, search, replace, and transform text in one line. These are the tools you'll use in literally every project.
What are JavaScript String Methods?
JavaScript strings are objects with built-in methods. Like Python, they are immutable โ methods return new strings, they don't change the original.
- โข
.toUpperCase()/.toLowerCase() - โข
.trim()โ remove whitespace from both ends - โข
.includes(str)โtrue/false - โข
.startsWith(str)/.endsWith(str) - โข
.indexOf(str)โ first index, or -1 - โข
.slice(start, end)โ extract substring - โข
.replace(old, new)โ replaces first match;.replaceAll(old, new) - โข
.split(separator)โ array;array.join(sep)โ string - โข
.padStart(len, char)/.padEnd()โ pad with characters
Think of it like editing a TikTok caption
Before posting, TikTok trims your caption (.trim()), lowercases hashtags for matching (.toLowerCase()), splits them out (.split('#')), checks for banned words (.includes()), and replaces profanity (.replace()). One string, a chain of methods.
How It Works
- โข1. Strings are primitives but JS auto-boxes them into String objects to access methods
- โข2. Each method returns a new string โ chain them:
str.trim().toLowerCase() - โข3.
.slice(0, 3)extracts chars at index 0, 1, 2 (stop is exclusive) - โข4. Negative indices work in
.slice():.slice(-3)gets last 3 chars - โข5.
.replace(/regex/g, newStr)uses a regex for global replace across the whole string - โข6. Template literals use
`${expr}`โ technically not a method but used everywhere
Real-World Examples
- โขSearch:
title.toLowerCase().includes(query.toLowerCase())โ case-insensitive match - โขDisplay name:
username.slice(0, 15) + 'โฆ'โ truncate long names - โขURL slug:
title.toLowerCase().replaceAll(' ', '-') - โขPhone validation:
phone.replace(/\D/g, '')โ strip all non-digits - โขLeaderboard rank:
String(rank).padStart(3, '0')โ'007'
Key Facts
- โขStrings in JS are sequences of UTF-16 code units โ emoji can be 2 units, making
.lengthsurprising - โข
.at(-1)is the modern way to get the last character (cleaner than[str.length-1]) - โขRegular expressions unlock powerful pattern matching โ
/\d+/gmatches all digit sequences - โข
String.raw`\n`returns the literal two-character string\n, not a newline
Watch Out!
.replace(old, new) only replaces the first match. To replace ALL occurrences, use .replaceAll(old, new) or the regex flag: .replace(/old/g, new). Many beginners miss this and wonder why only one word changed.
Remember
String methods return new strings โ always assign the result. Chain methods for clean transformations: input.trim().toLowerCase().replace(' ', '-').
What You Learned
- โขCore methods:
.trim(),.includes(),.slice(),.replace(),.split() - โขStrings are immutable โ methods return new strings, always assign
- โขUnlocks: search, URL slugs, validation, text truncation, any user-facing text processing
Key Facts
- โStrings in JS are sequences of UTF-16 code units โ emoji can be 2 units, making
.lengthsurprising - โ
.at(-1)is the modern way to get the last character (cleaner than[str.length-1]) - โRegular expressions unlock powerful pattern matching โ
/\d+/gmatches all digit sequences - โ
String.raw`\n`returns the literal two-character string\n, not a newline
Real-World Examples
Remember
String methods return new strings โ always assign the result. Chain methods for clean transformations: input.trim().toLowerCase().replace(' ', '-').
Quick Quiz
.trim() does?