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.