TalentJR

🟨 JavaScript tutorial for beginners · Class 8 · Lesson 11 of 18 · Free

Template Literals & Strings in JavaScript – Build Text Easily

⏱ About 30 minutes · ▶ run real JavaScript here — nothing to install · no sign-in

In this lesson you will

  • Build text with backticks and ${ } instead of + joining
  • Put calculations and multi-line text inside template literals
  • Use string methods like toUpperCase, includes, slice, split, trim and padStart

You want your app to say "Asha scored 92 in Maths – Grade A+!" for every student. Gluing text with + signs and spaces is slow and easy to get wrong. Template literals let you write the sentence once and drop the values straight in.

What is a template literal in JavaScript?

A template literal is a string written with backticks ` ` instead of quotes. The backtick key is usually at the top-left of the keyboard, next to 1.

Inside backticks, ${ } is a slot. Whatever variable you put inside is dropped into the text.

Compare the two lines below. Both print the same thing, but the template literal is easier to read and you cannot forget a space.

🎬 How JavaScript thinks — step 1 of 7JavaScript is about to run line 1
▶const name = "Asha";
2const marks = 92;
3const old = "Hi " + name + ", you scored " + marks + "!";
4const easy = `Hi ${name}, you scored ${marks}!`;
5console.log(old);
6console.log(easy);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Hi Asha, you scored 92!
Hi Asha, you scored 92!

💡 Try it: change name to your own name and marks to any number.

Can I do maths or call functions inside ${ } in JavaScript?

Yes. Anything that gives a value can go in ${ }: a sum, a method call, even a short if/else using ? : (from Lesson 9).

toFixed(2) shows a number with exactly 2 digits after the decimal point — perfect for money.

Here we make a canteen bill.

🟨 JavaScript — you can edit this code
Console output
4 x Samosa = ₹60
With 5% tax: ₹63.00
Item in capitals: SAMOSA
Status: Big order

How do I write a multi-line string in JavaScript?

With quotes, a string must stay on one line (or you write \n for a new line). With backticks, just press Enter — the new lines are kept.

This makes receipts, poems and messages easy to write.

🟨 JavaScript — you can edit this code
Console output
--- Report Card ---
Name: Kabir
Maths: 88
Science: 79
Total: 167
Line 1
Line 2 (made with \n)

What are the most useful string methods in JavaScript?

Strings have many built-in methods. They never change the original string — they give back a NEW one. So store the result if you need it.

trim() removes extra spaces at the ends. includes() checks for a word. slice(start, end) cuts out a part (end not included). split() breaks a string into an array.

A string also has .length and indexes, just like an array: word[0] is the first letter.

🎬 How JavaScript thinks — step 1 of 11JavaScript is about to run line 1
▶const raw = " Diwali Mela ";
2const event = raw.trim();
3console.log(`[${raw}] → [${event}]`);
4console.log(event.length, event[0]);
5console.log(event.toUpperCase(), event.toLowerCase());
6console.log(event.includes("Mela"));
7console.log(event.slice(0, 6));
8console.log(event.replace("Mela", "Party"));
9console.log("Asha,Ravi,Meera".split(","));
10console.log("7".padStart(3, "0"));
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
[ Diwali Mela ] → [Diwali Mela]
11 D
DIWALI MELA diwali mela
true
Diwali
Diwali Party
["Asha", "Ravi", "Meera"]
007

How do I go through a string letter by letter?

for…of works on strings too. Each round gives you one letter.

Here we count vowels. toLowerCase() first means "A" and "a" are both counted. "aeiou".includes(ch) checks if the letter is a vowel.

🎬 How JavaScript thinks — step 1 of 26JavaScript is about to run line 1
▶const word = "Ahmedabad";
2let vowels = 0;
3for (const ch of word.toLowerCase()) {
4 if ("aeiou".includes(ch)) {
5 vowels++;
6 }
7}
8console.log(`${word} has ${vowels} vowels and ${word.length} letters.`);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Ahmedabad has 4 vowels and 9 letters.

How do I use template literals to change text on a web page?

Template literals and the DOM (Lesson 4) make a great pair. Type a name, pick a festival, and click the button.

The card's text is built with one template literal. trim() makes sure extra spaces do not spoil it.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it
Console output
Card says: Happy Diwali, Meera! 🎉

💡 a || "Friend" uses "Friend" when the box is empty, because an empty string counts as false.

🤔 Predict the output

Think like JavaScript: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

const a = 5;
const b = 3;
console.log(`${a} + ${b} = ${a + b}`);

Question 2. What does this print?

console.log("Score: " + 4 + 6);

Question 3. What does this print?

const s = "Cricket";
console.log(s.slice(0, 3).toUpperCase());

🧩 Your challenge

Make a cricket scoreboard. For each player in the array, print one line with a template literal: the name in CAPITALS, runs, balls, and the strike rate (runs ÷ balls × 100) with 1 decimal place. Expected output: ROHIT: 57 runs off 40 balls (SR 142.5) GILL: 30 runs off 32 balls (SR 93.8) PANT: 12 runs off 8 balls (SR 150.0)

Expected output in the console:
ROHIT: 57 runs off 40 balls (SR 142.5)
GILL: 30 runs off 32 balls (SR 93.8)
PANT: 12 runs off 8 balls (SR 150.0)
🟨 JavaScript — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

const name = "Asha";
console.log("Hello, ${name}!");

No error — but it prints Hello, ${name}! with the dollar sign, not the name.

✅ Right

const name = "Asha";
console.log(`Hello, ${name}!`);

${ } only works inside backticks. In normal quotes it is just plain text.

❌ Wrong

const city = "pune";
console.log(city.toUppercase());

TypeError: city.toUppercase is not a function

✅ Right

const city = "pune";
console.log(city.toUpperCase());

JavaScript is case-sensitive. The method is toUpperCase, with a capital C.

❌ Wrong

let team = "india";
team.toUpperCase();
console.log(team);

No error — but it still prints india in small letters, because the result was thrown away.

✅ Right

let team = "india";
team = team.toUpperCase();
console.log(team);

String methods give back a new string; they never change the old one. Store the result.

📌 Remember

  • Backticks ` ` make a template literal; ${ } drops a value into the text.
  • Any expression works in ${ }: maths, method calls, a ? b : c.
  • Backtick strings can span many lines.
  • "Score: " + 4 + 6 gives "Score: 46" — use ${4 + 6} for maths.
  • String methods (trim, slice, split, toUpperCase…) return a NEW string.
  • Strings have length and indexes, and for…of goes letter by letter.

Finished the quiz and the challenge?

Questions students ask

What is the difference between quotes and backticks in JavaScript?

"Double" and 'single' quotes make normal strings. Backticks make template literals, which allow ${ } slots and real new lines. For plain text, all three work.

Where is the backtick key on the keyboard?

On most keyboards it is at the top-left, just below Esc and left of the 1 key. It shares the key with the ~ sign.

Can I change one letter of a string in JavaScript?

No. Strings cannot be changed in place, so word[0] = "B" does nothing (and gives an error in strict mode). Make a new string instead, for example "B" + word.slice(1).

Why does toFixed give a string?

toFixed is for showing a number neatly, so it returns text like "93.8". If you need to do more maths, use the original number or Number(text).