iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

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

SyntaxNotesExample
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

EscapeMeans
\nNewline
\tTab
\" / \'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}!`;

Test yourself

Q1. Template literals are wrapped in…
Q2. Interpolate a variable with…
Q3. Backtick strings can span…

Discussion

Loading…