Strings & Template Literals
Learn JavaScript strings with template literals, backticks, interpolation and key string methods like slice, includes and trim.
On this page
1. What are strings?
JavaScript has three ways to write a string:
| Syntax | Example | Note |
|---|---|---|
| Single quotes | 'Hello' | Simple text |
| Double quotes | "Hello" | Simple text, same behavior |
| Backticks | `Hello ${name}` | Template literal, interpolation and multi line |
2. Why does it matter?
In old code you had to join text and variables with +. In long strings it was hard to manage quotes and spaces. Template literals made this much cleaner.
| Old concatenation | Template literal |
|---|---|
| A plus and quotes for every variable | A dollar sign and braces put the variable right in |
| Forgetting spaces is common | The text is written the way it will look |
| Backslash n for multi line | A new line directly inside backticks |
| Long strings are hard to read | An HTML snippet or message is easy to see |
3. Where is it used?
| Real use | String feature |
|---|---|
| Welcome message | Template literal: `Welcome, ${user.name}` |
| Search filter | includes(), toLowerCase() |
| Cleaning form input | trim() |
| Splitting CSV or tags | split(",") |
| Formatting an order ID | padStart(6, "0") |
| Making a URL slug | toLowerCase(), replaceAll(" ", "-") |
4. How does it work?
A string is a sequence of characters. Every character has an index that starts at 0.
Strings are immutable. Every method returns a new string and the original stays the same.
5. Code example
Template literals:
const name = "Usman";
const items = 3;
const price = 450;
const message = `Hello ${name}! You have ${items} items in your cart.
Total: Rs ${items * price}`;
console.log(message);Any expression can go inside ${...}, like a calculation or a function call.
Common methods:
const raw = " JavaScript Is Easy To Learn ";
const text = raw.trim();
console.log(text.length);
console.log(text.toLowerCase());
console.log(text.includes("Easy"));
console.log(text.startsWith("Java"));
console.log(text.slice(0, 10));
console.log(text.split(" "));
console.log(text.replaceAll(" ", "-"));
console.log(text.at(-1));
console.log("7".padStart(4, "0"));Output: 27, "javascript is easy to learn", true, true, "JavaScript", ["JavaScript", "Is", "Easy", "To", "Learn"], "JavaScript-Is-Easy-To-Learn", "n", "0007".
How does the code work?
| Method | Job |
|---|---|
trim() | Removes spaces from the start and end |
length | The number of characters (a property, not a method) |
includes() | Whether the text is inside, true / false |
slice(0, 10) | The part from index 0 to 9, the end is not included |
split(" ") | Breaks the string into an array |
replaceAll() | Replaces every match |
at(-1) | A character counted from the back with a negative index |
padStart(4, "0") | Adds "0" at the start to make the length 4 |
Interview tip
"How do you reverse a string?" The common answer: [...str].reverse().join(""). Spread is better than split("") because characters like emoji are separated correctly.
6. Common mistakes
Mistake: Expecting a method to change the original
Why? Strings are immutable. Writing name.toUpperCase() does not change name itself.
Better: Save the result: const upper = name.toUpperCase().
Mistake: Using normal quotes for a template literal
Why? In single or double quotes, the dollar sign and braces are printed as plain text and the value does not appear.
Better: Always use backticks for interpolation.
Mistake: Searching without thinking about case
Why? includes() is case sensitive, so a search for 'Lahore' does not match 'lahore'.
Better: Apply toLowerCase() to both sides before comparing.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Data Types, Operators |
| After this | Numbers & Math, Conditionals |
| Further on | Array Methods, Regular Expressions |
Final quick revision
| Point | Detail |
|---|---|
| Three syntaxes | Single quotes, double quotes and backticks |
| Template literal | Backticks + ${expression} |
| Index | Starts at 0, at(-1) is the last |
| Immutable | Methods return a new string |
| Key methods | trim, includes, slice, split, replaceAll |