JS Strings
A string is a sequence of characters. JavaScript has three quote styles, full Unicode support, and a rich set of methods on the string prototype.
The three quote styles
| Syntax | Notes | Example |
|---|---|---|
Double "…" | Standard. No interpolation. | "hello" |
Single '…' | Identical to double — pick one for the project. | 'hello' |
Backtick ` … ` | Template literal — supports interpolation and newlines. | `Hi, ${name}!` |
Template literals
JS
const name = "Ada", role = "admin";
// Interpolation
const greeting = `Hi, ${name}! You're an ${role}.`;
// Multi-line (no \n needed)
const html = `
<article>
<h1>${name}</h1>
</article>
`;
// Tagged template — function processes the parts
const safe = sql`SELECT * FROM users WHERE name = ${name}`;
Escape sequences
| Escape | Means |
|---|---|
\n | Newline |
\t | Tab |
\" / \' | Embed a quote that matches the wrapper |
\\ | A literal backslash |
é | Unicode code point (é) |
Tip: Default to backticks. They handle everything single/double quotes do plus interpolation — at zero performance cost.
Example
Example
<!DOCTYPE html>
<html>
<body>
<p id="out"></p>
<script>
document.getElementById("out").textContent = "Hello from JS Strings!";
</script>
</body>
</html>
Try it Yourself »
Exercise
Build a greeting with a template literal.
const msg = `Hello,
{name}!`;
Single character that starts interpolation.
Discussion
Loading…