JavaScript strings: Quotes, escapes, and template literals

Create a JavaScript string with single quotes, double quotes, or backticks. Single and double quotes give the same value for the same content. Use backticks for a template literal when you need to insert an expression or include a line break directly.

Choose single or double quotes

The opening and closing quotes mark the string’s bounds. They are separate from quotes that belong to the value. A single-quoted string can contain double quotes directly, and a double-quoted one can contain an apostrophe. "" is an empty string. "12" and 12 have different types.

const first = 'Hello';
const second = "Hello";
console.log(first === second); // true

const quoted = 'They said "Start".';
const apostrophe = "It's ready.";

Escape quotes, backslashes, and line breaks

To include the same quote as the delimiter, put a backslash before it. Write \\ for one literal backslash and \n for a newline. In a Windows path with a single backslash before notes, \n becomes a newline. Check the syntax in the string literal grammar.

const both = 'They said "It\'s ready".';
const path = 'C:\\notes\\work';
const twoLines = 'First line\nSecond line';

console.log(both);
console.log(path);
console.log(twoLines);

Use backticks for expressions and multiline strings

A template literal evaluates the expression in ${...} and inserts its value. It also preserves line breaks and indentation you type between the backticks, so indentation in your source can become part of the output.

const name = 'Jamie';
const greeting = `Hello, ${name}`;
console.log(greeting);

const lines = `First line
Second line`;
console.log(lines);

To display ${name} literally, put a backslash before the dollar sign. To include a backtick, escape it too. Run the example in the console: it prints the literal placeholder even without a name variable. The escape characters do not appear in the resulting string. See the template literal grammar.

{
  const example = `Use \${name} inside \`backticks\`.`;
  console.log(example); // Use ${name} inside `backticks`.
}

Compare actual strings with their JSON representation

Save the complete code as string-example.html in UTF-8 and open it in a browser. Choose a sample to compare its actual value, its JSON.stringify() representation, and the result of typeof. The example uses textContent to display literal text, without treating it as HTML markup.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare string representations</title>
</head>
<body>
<label for="sample">String sample</label>
<select id="sample">
  <option value="single">Single quotes</option>
  <option value="double">Double quotes</option>
  <option value="both">Both kinds of quotes</option>
  <option value="path">Path with backslashes</option>
  <option value="newline">Line break</option>
  <option value="template">Template literal</option>
</select>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite"></pre>
<script>
const name = 'Jamie';
const samples = {
  single: 'It\'s ready.',
  double: "They said \"Start\".",
  both: 'They said "It\'s ready".',
  path: 'C:\\notes\\work',
  newline: 'First line\nSecond line',
  template: `Hello, ${name}`,
};

const select = document.querySelector('#sample');
const output = document.querySelector('#output');
function show() {
  const value = samples[select.value];
  output.textContent = `Value:\n${value}\n\nJSON representation: ${JSON.stringify(value)}\nType: ${typeof value}`;
}
select.addEventListener('change', show);
document.querySelector('#reset').addEventListener('click', () => {
  select.value = 'single';
  show();
});
show();
</script>
</body>
</html>

For the path, the value displays single backslashes while the JSON representation displays each twice. For the newline option, the value spans two lines while JSON shows \n. Reset returns to the single-quote sample.