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 1Line 2Line 3`;console.log(multi);
Output
Line 1
Line 2
Line 3
Your output
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!
Your output
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?
Strings are immutable in memory
Predict the outputjavascript
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
You predicted
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
Your output
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!
Your output
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);