Strings and Text
Master text in code: string creation, concatenation, slicing, and common methods every developer uses daily.
What you'll learn
- How to create and format strings with quotes and template literals
- String concatenation and interpolation techniques
- Accessing characters and substrings with indexing and slicing
- Common string methods: length, split, replace, trim, includes
- Why string immutability matters for performance and safety
Concept
Strings and Text
A string is a sequence of characters — text, in everyday terms. Strings are everywhere in programming: user input, file contents, URLs, error messages, and JSON data are all strings. Because text is the most common data type you will handle, mastering string operations is essential.
Creating Strings
JavaScript lets you create strings with single quotes, double quotes, or backticks. Backticks create template literals, which support multi-line text and interpolation — embedding expressions directly inside the string with ${}:
const single = 'Hello';
const double = "World";
const name = "Ada";
const greeting = `Hello, ${name}!`; // "Hello, Ada!"
Concatenation and Interpolation
Concatenation joins strings with +. Interpolation embeds values inside a template literal. Interpolation is almost always clearer, especially when combining multiple values:
const user = "Grace";
const count = 5;
// Concatenation — works but gets messy:
const msg1 = "User " + user + " has " + count + " items.";
// Interpolation — cleaner and more readable:
const msg2 = `User ${user} has ${count} items.`;
Accessing and Slicing
You can access individual characters by index (starting at 0) and extract substrings with slice, substring, or substr:
const text = "JavaScript";
text[0]; // "J"
text.length; // 10
text.slice(0, 4); // "Java"
text.slice(-6); // "Script" (negative = from end)
Common Methods
Strings come with built-in methods — functions attached to the string object. The most frequently used ones include toUpperCase(), toLowerCase(), trim(), split(), replace(), includes(), and indexOf(). These methods return *new* strings; they never modify the original, because strings are immutable in JavaScript.
Immutability
Immutability means a string cannot be changed after creation. Methods like toUpperCase() return a brand-new string rather than altering the original. This is a safety feature: it prevents accidental mutations and makes string behavior predictable. When you need to "modify" a string, you reassign the variable to the result of the method call.
Understanding strings deeply will save you countless hours. Most real-world bugs in web applications involve text processing — parsing input, validating formats, or transforming data for display.
Example
const sentence = " The quick brown fox ";
// Trim whitespace from both ends
const clean = sentence.trim(); // "The quick brown fox"
// Split into words, then reverse and rejoin
const words = clean.split(" "); // ["The", "quick", "brown", "fox"]
const reversed = words.reverse().join(" "); // "fox brown quick The"
// Replace a word (only the first match)
const updated = clean.replace("brown", "red"); // "The quick red fox"
// Check if the string contains a substring
const hasFox = clean.includes("fox"); // true
// Extract a portion
const middle = clean.slice(10, 15); // "brown"
console.log(reversed, updated, hasFox, middle);
This example demonstrates trimming, splitting, reversing, replacing, searching, and slicing — six of the most common string operations you will use in real projects.
Try it
- Case Converter
Transform text between camelCase, snake_case, kebab-case and more — pure string manipulation in action.
- Text Counter
Count characters, words, and lines in any text. Internally it uses split, length, and trim.
Common mistakes
The mistake
Expecting string methods to modify the original variable.
The fix
Strings are immutable. Methods return a new string. Always capture the result: text = text.trim(), not just text.trim().
The mistake
Using + to build complex strings instead of template literals.
The fix
Use template literals (backticks) with ${} interpolation. They are more readable and less error-prone than chaining + operators.
The mistake
Forgetting that string indexing starts at 0.
The fix
The first character is text[0], not text[1]. The last is text[text.length - 1]. Off-by-one errors in string slicing are extremely common.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Complete reference for all string methods and template literal syntax.
Practice Strings and Text
10 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.