JavaScript Strings

Creating strings, escaping and template literals.

✍️ 3 exercises + code challenge in this lesson

A string stores text, written in single quotes, double quotes or backticks.

  • .length gives the number of characters.
  • Escape special characters with a backslash: \", \', \n (new line).

Template literals

Backtick strings can span multiple lines and insert values with ${...}:

`Hello ${name}, you are ${age} years old`

Key Takeaways: JavaScript Strings

Mastering JavaScript Strings helps you write clean and reliable JavaScript code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

JavaScript Strings Code Example

JavaScript
<pre id="out"></pre>
<script>
const log = (...a) => document.getElementById("out").textContent += a.join(" ") + "\n";
const name = "Ali";
const course = 'JavaScript';
const quote = "He said \"Hello\"";

log(name.length, "characters in", name);
log(quote);
log(`${name} is learning ${course} and has finished ${3 * 4} lessons.`);
log(`Line one
Line two`);
</script>
Free sign-in required · Google login available

JavaScript Strings Practice Exercises

Check what you learned in “JavaScript Strings”.

/ 3 solved
Fill in the blank

Insert the variable with a template literal.

const msg = `Hello, !`;
✓ Correct! Not quite. Try again.
Multiple choice

Which property gives the length of a string?

✓ Correct! Not quite. Try again.
Code challenge

Given name = "Sara" and age = 25, log "Sara is 25" using a template literal.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services