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.
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);
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);
const name = 'Mina';creates a string with the charactersM,i,n, anda.const message = "Mina's score is 10";creates another string. Double quotes are used so the apostrophe inMina'sdoes not need escaping.name === "Mina"compares the value innamewith a double-quoted string. Quote choice does not affect the value, so the comparison istrue.- The first
console.logprintsMina's score is 10. - The second
console.logprintstrue.
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.
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) {
;
}
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:
Comparisons
| Form | Delimiter | Creates an ordinary string? | Interpolates ${value}? | Best use |
|---|---|---|---|---|
| Single-quoted string | 'text' | Yes | No | Ordinary text; convenient when text contains " |
| Double-quoted string | "text" | Yes | No | Ordinary text; convenient when text contains ' |
| Template literal | `text` | Yes | Yes | Dynamic or multiline text |
Single quotes vs double quotes
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.
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.
Keep learning
Related questions
Abort Ajax Requests with jQuery jqXHR.abort()
Learn how to cancel an in-progress jQuery Ajax request with jqXHR.abort(), handle abort status safely, and avoid stale UI updates.
Access the Correct this Inside a JavaScript Callback
Learn why JavaScript this changes in callbacks and how to preserve an object context using bind, arrow functions, and event handler patterns.
Add Key-Value Pairs to JavaScript Objects
Learn how to add key-value pairs to JavaScript objects with dot and bracket notation, dynamic keys, examples, and common mistakes.