Question

JavaScript Single vs Double Quotes for String Literals

Question

Consider these two JavaScript statements:

console.log("double");
console.log('single');

The first string literal uses double quotes, while the second uses single quotes. Are single and double quotes interchangeable for JavaScript string literals? If they are not completely interchangeable, when is one preferable to the other?

Short Answer

In JavaScript, single-quoted and double-quoted string literals behave the same in almost all cases. The practical difference is how you escape quote characters contained inside the string. Most projects choose one style and enforce it consistently with a formatter or linter.

Concept

A string literal is text written directly in source code. JavaScript supports both single quotes ('...') and double quotes ("...") to create ordinary strings.

const first = 'Hello';
const second = "Hello";

console.log(first === second); // true

Both values are strings containing exactly the same characters. Neither form is faster, more modern, or more capable than the other.

The important rule is that the quote used to start a string must normally also end it. If that same quote character appears inside the text, escape it with a backslash (\) or choose the other quote style.

const contraction = "Don't stop";
const quotedWord = 'She said "yes"';

Quote style matters mainly for readability and consistency. In a shared codebase, developers commonly let a tool such as Prettier or ESLint choose and enforce the project's convention.

JavaScript also has a third string-literal form: template literals, written with backticks. They are especially useful for interpolation and multiline text, not simply as another preferred quote style.

Mental Model

Think of quotes as two kinds of matching containers for text: single-quote containers and double-quote containers.

  • A string opened with ' should be closed with '.
  • A string opened with " should be closed with ".
  • Put a quote of the other kind inside without extra work.

For example, if your sentence contains an apostrophe, double quotes make the container easier to read:

const message = "We're ready";

If it contains double-quoted speech, single quotes may be clearer:

const message = 'The button says "Save"';

The data inside the container is the same; only the source-code spelling changes.

Take Quiz

Syntax and Examples

Both forms create ordinary JavaScript strings:

const singleQuoted = 'JavaScript';
const doubleQuoted = "JavaScript";

console.log(singleQuoted); // JavaScript
console.log(doubleQuoted); // JavaScript
console.log(singleQuoted === doubleQuoted); // true

Use the opposite quote type inside a string when possible:

const userMessage = "Don't forget your password.";
const label = 'Click the "Continue" button.';

If you must use the matching quote inside the string, escape it with \:

const escapedSingle = 'Don\'t forget your password.';
const escapedDouble = "Click the \"Continue\" button.";

The backslash is used in source code to represent a character that would otherwise end the string. It is not included in the resulting text:

console.log(escapedSingle); // Don't forget your password.

For variables embedded in text, use a template literal with backticks:

const name = 'Ava';
const greeting = `Hello, ${name}!`;

console.log(greeting); // Hello, Ava!

Single and double quotes do not interpolate ${...}:

const name = 'Ava';
console.log('Hello, ${name}!'); // Hello, ${name}!

Step by Step Execution

Trace this example:

const name = 'Mina';
const message = "Mina's score is 10";
const equalText = name === "Mina";

console.log(message);
console.log(equalText);
  1. const name = 'Mina'; creates a string with the characters M, i, n, and a.
  2. const message = "Mina's score is 10"; creates another string. Double quotes are used so the apostrophe in Mina's does not need escaping.
  3. name === "Mina" compares the value in name with a double-quoted string. Quote choice does not affect the value, so the comparison is true.
  4. The first console.log prints Mina's score is 10.
  5. The second console.log prints true.

Real World Use Cases

Single and double quotes appear in the same kinds of programming tasks:

  • UI text: const heading = 'Account settings';
  • API payloads: const status = "pending";
  • Error messages with apostrophes: throw new Error("User isn't authorized");
  • Text containing quoted labels: const help = 'Select "Export" to download a file.';
  • HTML snippets in small scripts: choosing an outer quote type can reduce escaping.
const html = '<button aria-label="Close">×</button>';

In larger applications, quote style is usually selected once for the entire repository. The business logic should not depend on whether the source used single or double quotes.

Take Quiz

Real Codebase Usage

Professional JavaScript projects generally treat quote choice as a formatting convention.

Use a consistent project style

A project may prefer single quotes:

const apiPath = '/api/users';
const method = 'GET';

Or it may prefer double quotes:

const apiPath = "/api/users";
const method = "GET";

Both are valid. Consistency makes reviews and version-control diffs easier to read.

Let formatting tools decide

Teams often use Prettier to rewrite files into a chosen style automatically. ESLint can report inconsistent quotation marks. This avoids spending code-review time debating a preference.

Avoid unnecessary escaping

Even with a project-wide convention, formatters and developers may use the alternate delimiter when it prevents clutter:

const warning = "You can't undo this action.";

Use template literals only when their features help

For dynamic values, interpolation is clearer than concatenation:

function buildUserPath(id) {
  return `/api/users/${id}`;
}

Do not switch to backticks merely because they look different; ordinary fixed text is often clearer with the project's normal quote style.

Common Mistakes

Forgetting to escape a matching quote

This ends the string too early and causes a syntax error:

// SyntaxError
const message = 'It's ready';

Use double quotes or escape the apostrophe:

const message = "It's ready";
// or
const sameMessage = 'It\'s ready';

Mixing opening and closing quote types

The delimiters must match:

// SyntaxError
const color = 'blue";

Correct version:

const color = 'blue';

Expecting quote type to change the value

These are equal values, not different types:

console.log('42' === "42"); // true

However, a string is still different from a number:

console.log('42' === 42); // false

Expecting interpolation in ordinary quoted strings

const user = 'Noah';
const text = 'Hello, ${user}!';

console.log(text); // Hello, ${user}!

Use backticks for interpolation:

const text = `Hello, ${user}!`;

Confusing source-code quotes with JSON rules

JavaScript allows both quote styles, but valid JSON requires double quotes around property names and string values:

{"name":"Noah"}

This is JavaScript code, but it is not valid JSON:

{ name: 'Noah' }

Comparisons

FormDelimiterCreates an ordinary string?Interpolates ${value}?Best use
Single-quoted string'text'YesNoOrdinary text; convenient when text contains "
Double-quoted string"text"YesNoOrdinary text; convenient when text contains '
Template literal`text`YesYesDynamic or multiline text

Single quotes vs double quotes

QuestionAnswer
Do they produce different string types?No. Both produce primitive string values.
Is one faster?No meaningful performance advantage should guide the choice.
Can either contain escape sequences such as \n?Yes.
Which should a team use?The style configured by its formatter, linter, or existing codebase.

Strings vs template literals

const product = 'Notebook';
const withConcatenation = 'Item: ' + product;
const withTemplateLiteral = `Item: ${product}`;

Template literals improve readability when values must be inserted. For fixed text, 'Item', "Item", and `Item` can all work, but normal quotes communicate that no interpolation is needed.

Take Quiz

Cheat Sheet

// Equivalent ordinary strings
const a = 'hello';
const b = "hello";

// Put the other quote type inside directly
const apostrophe = "Don't panic";
const speech = 'She said "hello"';

// Escape a matching delimiter
const escapedApostrophe = 'Don\'t panic';
const escapedSpeech = "She said \"hello\"";

// Common escape sequences
const newline = 'first line\nsecond line';
const tab = 'name:\tAda';
const backslash = 'C:\\temp';

// Interpolation requires backticks
const name = 'Ada';
const greeting = `Hello, ${name}!`;
  • 'text' and "text" are interchangeable for ordinary JavaScript strings.
  • Choose the delimiter that avoids escapes, unless your project formatter chooses for you.
  • Matching delimiters inside text must be escaped with \.
  • JSON requires double-quoted strings and keys.
  • Backticks create template literals; use them for ${...} interpolation or multiline text.

FAQ

Are single quotes and double quotes the same in JavaScript?

For ordinary string literals, yes. They create the same kind of string value. Their main difference is which quote character needs escaping inside the text.

Should I use single or double quotes in JavaScript?

Use the convention already used by the project, ideally enforced by Prettier or ESLint. If there is no convention, choose one and stay consistent.

Are single quotes faster than double quotes in JavaScript?

No practical speed difference should influence your choice. Pick based on readability and project consistency.

Can I put an apostrophe inside a single-quoted JavaScript string?

Yes, but escape it: 'Don\'t'. Often it is clearer to use double quotes instead: "Don't".

Why does ${name} not work inside single quotes?

Interpolation is a template-literal feature. Use backticks: `Hello, ${name}`.

Can I use single quotes in JSON?

No. Standard JSON requires double quotes for both property names and string values.

Do double and single quotes work the same in every language?

No. Some languages assign different meanings to them. This equivalence is specific to JavaScript ordinary string literals.

Take Quiz

Mini Project

Description

Build a small message formatter for a support dashboard. It receives a customer's name, a feature name, and a status, then returns readable messages. The exercise demonstrates choosing quote delimiters to avoid unnecessary escapes and using template literals when values are dynamic.

Goal

Create a function that returns a clear support message for approved, rejected, and unknown statuses.

Requirements

Use a formatStatusMessage(name, feature, status) function. Handle the statuses approved and rejected with different messages. Include the customer's name and feature in each returned message. Include an apostrophe in at least one message without unnecessary escaping. Return a fallback message for an unrecognized status.

Take Quiz

Keep learning

Related questions

View more