Skip to content
BasicsChapter 8 / 47Beginner4 min read

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:

SyntaxExampleNote
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 concatenationTemplate literal
A plus and quotes for every variableA dollar sign and braces put the variable right in
Forgetting spaces is commonThe text is written the way it will look
Backslash n for multi lineA new line directly inside backticks
Long strings are hard to readAn HTML snippet or message is easy to see

3. Where is it used?

Real useString feature
Welcome messageTemplate literal: `Welcome, ${user.name}`
Search filterincludes(), toLowerCase()
Cleaning form inputtrim()
Splitting CSV or tagssplit(",")
Formatting an order IDpadStart(6, "0")
Making a URL slugtoLowerCase(), replaceAll(" ", "-")

4. How does it work?

A string is a sequence of characters. Every character has an index that starts at 0.

Index model
String: "Lahore"
L=0, a=1, h=2, o=3, r=4, e=5
length = 6, last index = 5
at(-1) gives the last character "e"

Strings are immutable. Every method returns a new string and the original stays the same.

5. Code example

Template literals:

js
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:

js
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?

MethodJob
trim()Removes spaces from the start and end
lengthThe 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

Final quick revision

PointDetail
Three syntaxesSingle quotes, double quotes and backticks
Template literalBackticks + ${expression}
IndexStarts at 0, at(-1) is the last
ImmutableMethods return a new string
Key methodstrim, includes, slice, split, replaceAll