Strings and Template Literals

JavaScript ES2024 · ✓ verified by execution on 2026-08-07

Strings are sequences of characters used to represent text in JavaScript.

Strings are represented fundamentally as sequences of UTF-16 code units.

Creating Strings

You can create strings using single quotes or double quotes.

javascript
const single = 'Hello';
const double = "World";
console.log(single + ' ' + double);
Output
Hello World

Template Literals

Template literals are enclosed by backtick (`) characters instead of double or single quotes.

Template literals can contain placeholders. These are indicated by the dollar sign and curly braces (${expression}).

javascript
const name = 'Alice';
const age = 30;
console.log(`My name is ${name} and I am ${age} years old.`);
Output
My name is Alice and I am 30 years old.

A common misconception is that you can interpolate variables inside single or double quotes using ${}. See what happens when you try:

javascript
const user = 'Alice';
console.log('Welcome, ${user}!');
Output
Welcome, ${user}!

You can embed any valid JavaScript expression inside a template literal.

javascript
const price = 10;
const tax = 0.2;
console.log(`Total: $${price + (price * tax)}`);
Output
Total: $12

Multi-line strings are also easy with template literals.

Any newline characters inserted in the source are part of the template literal.

javascript
const multi = `Line 1
Line 2
Line 3`;
console.log(multi);
Output
Line 1
Line 2
Line 3

Escaping Characters

If you need to include a quote inside a string delimited by the same quote, you can escape it.

javascript
console.log('It\'s a beautiful day!');
Output
It's a beautiful day!

String Immutability

Strings are immutable: once a string is created, it is not possible to modify it.

Why Strings Cannot Be Changed

You might wonder why strings are immutable in JavaScript. Editing one character in place — text[0] = 'H' — would obviously be cheaper than building a new string. The catch is that engines share string storage: ten variables holding "hello" frequently point at one place in memory. Were strings mutable, changing one would change all ten.

Immutability is what makes that sharing safe. When you genuinely need to edit characters, split the string into an array, change what you need, and join it back — arrays are mutable, strings are not.

Because of immutability, a common trap is assuming that string methods modify the original string directly. What do you think this code outputs?

let word = "bat" Memory Location A "bat" word.toUpperCase() Memory Location B (New!) "BAT" Unassigned, goes to garbage Original is untouched
Strings are immutable in memory
Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

let word = 'b';
word.toUpperCase();
word += 'at';
console.log(word);
Output
bat

String methods like toUpperCase() return a new string rather than modifying the existing one. You must assign the result back to a variable if you want to keep it!

javascript · visualize
let text = 'hello';
text = text.toUpperCase();
console.log(text);
HELLO

Accessing Code Units

You can access the exact UTF-16 code unit of a character.

javascript
const str = 'A';
console.log(str.charCodeAt(0));
Output
65

Tagged Templates

A more advanced form of template literals are tagged templates.

You can pass a template literal directly to a function, called a tag function.

javascript
function tag(strings, ...values) {
  return 'Tagged!';
}
console.log(tag`Hello`);
Output
Tagged!

Check yourself

How do string methods like toUpperCase() affect the original string?

Reveal answer

They return a new string, leaving the original untouched. — Because strings are immutable, methods return a new modified string rather than altering the original.

What is the correct way to interpolate variables into a string?

Reveal answer

Using backticks (`) and ${} — Template literals must be enclosed in backticks (`) to support interpolation with ${}.

What happens if you press Enter to create a new line inside single quotes?

Reveal answer

It throws a SyntaxError. — Standard quotes do not support multi-line text, you must use backticks (`) or escape sequences.

Challenges

Challenge 1 +20 XP

Fix the syntax so the string properly greets the user by evaluating the variable instead of printing the literal text '${user}'.

javascript
  • Test 1 — expects "Welcome, Bob!"
Need a hint? (−25% XP)

Remember to use backticks (`) instead of single quotes (') for interpolation.

Show solution (0 XP)
const user = 'Bob';
const greeting = `Welcome, ${user}!`;
console.log(greeting);

🐞 Bug Hunt +30 XP

Bug Hunt: The developer tried to uppercase the title, but it's still printing in lowercase! Fix the code so it prints 'CODE LUDO'.

This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.

javascript
  • Test 1 — expects "CODE LUDO"
Need a hint? (−25% XP)

String methods don't change the original string. You need to assign the returned value back to the variable.

Show solution (0 XP)
let title = 'Code Ludo';
title = title.toUpperCase();
console.log(title);