Strings & Template Literals
Strings — Text in JavaScript
A string is just a sequence of characters stored like a static array — letters, numbers, spaces, emojis, anything you can type. You can create strings using single quotes, double quotes, or backticks. Single and double quotes work the same way, but backticks unlock superpowers.
Think of strings like a necklace of beads. Each bead is a character, and you can count them, rearrange them, slice off a section, or check if a particular bead is in the necklace.
Strings are immutable in JavaScript — you can't change individual characters. Instead, string methods always return a new string.
Creating Strings & Basic Properties
Template Literals — Strings with Superpowers
Template literals use backticks (`) instead of quotes, and they let you do two amazing things:
- String interpolation with
${expression}— embed any JavaScript expression right inside your string. No more clunky concatenation! - Multi-line strings — just press Enter inside backticks, and the line break shows up in the output. No need for
\n.
Once you start using template literals, you'll never want to go back to + for building strings.
Template Literals in Action
Essential String Methods
Strings come loaded with useful methods. Remember, these never change the original string — they always return a new one.
.toUpperCase()/.toLowerCase()— change case.trim()— remove whitespace from both ends (great for user input!).includes(text)— check if a substring exists (returns true/false).indexOf(text)— find the position of a substring (-1 if not found).slice(start, end)— extract a portion of the string.split(separator)— chop a string into an array of pieces.replace(old, new)— swap the first occurrence; use.replaceAll()for all.startsWith(text)/.endsWith(text)— check the beginning or end
String Methods Playground
Quick check
Continue reading