๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
โšก JavaScript

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.

8 min 10 XP Lesson 10 of 21
String Methods
๐ŸŒ

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 .length surprising
  • โ€ข.at(-1) is the modern way to get the last character (cleaner than [str.length-1])
  • โ€ขRegular expressions unlock powerful pattern matching โ€” /\d+/g matches 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 .length surprising
  • โ†’.at(-1) is the modern way to get the last character (cleaner than [str.length-1])
  • โ†’Regular expressions unlock powerful pattern matching โ€” /\d+/g matches all digit sequences
  • โ†’String.raw`\n` returns the literal two-character string \n, not a newline

Real-World Examples

โ€ข Search: <code>title.toLowerCase().includes(query.toLowerCase())</code> โ€” case-insensitive match โ€ข Display name: <code>username.slice(0, 15) + 'โ€ฆ'</code> โ€” truncate long names โ€ข URL slug: <code>title.toLowerCase().replaceAll(' ', '-')</code> โ€ข Phone validation: <code>phone.replace(/\D/g, '')</code> โ€” strip all non-digits โ€ข Leaderboard rank: <code>String(rank).padStart(3, '0')</code> โ†’ <code>'007'</code>

Remember

String methods return new strings โ€” always assign the result. Chain methods for clean transformations: input.trim().toLowerCase().replace(' ', '-').

Quick Quiz

1 / 2

.trim() does?