Skip to main content

How to Put Quotes Inside a String in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A JavaScript string can start and end with single quotes, double quotes or backticks. To put one kind of quote inside a string, wrap the string in another kind, or put a backslash before the quote.

Syntax​

Syntax
'<text with "double quotes">'
"<text with 'single quotes'>"
"<text with \"escaped\" double quotes>"
`<text with 'both' "kinds">`
PartWhat it does
'...'A string in single quotes. Double quotes inside it are ordinary characters.
"..."A string in double quotes. Single quotes inside it are ordinary characters.
\" or \'A backslash makes the next quote part of the text instead of the end of the string.
`...`A template literal. Both kinds of quote are ordinary characters inside it.

MDN's String reference: "String literals can be specified using single or double quotes, which are treated identically, or using the backtick character `. This last form specifies a template literal: with this form you can interpolate expressions."

The Example​

The example writes a short piece of JSON, which is full of double quotes, inside single quotes. It escapes the double quotes of a second string with backslashes, and writes a third with a template literal that holds both kinds of quote.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which stores JSON with double quotes in single quotes, a sentence with escaped double quotes, and a template literal with an apostrophe and double quotes, then logs all three.

main.js
const json = '{"name":"Ada","role":"admin"}';
const escaped = "She said \"hi\" and left.";
const both = `It's "fine".`;

console.log(json);
console.log(escaped);
console.log(both);

Step 2: Run It​

Terminal
node main.js
Output
{"name":"Ada","role":"admin"}
She said "hi" and left.
It's "fine".

A zsh terminal where node main.js prints the JSON with its double quotes, She said "hi" and left., and It's "fine".

The command is the same in zsh, bash and PowerShell.

All three printed their quotes as ordinary text. The backslashes in the second string don't appear in the output: they only told JavaScript that those quotes belong to the text.

Common Mistakes​

MistakeWhat happensDo this instead
Putting a double quote inside a double-quoted stringThe string ends early: SyntaxError: Unexpected identifier 'hi'Wrap the string in single quotes, or write \"
An apostrophe inside a single-quoted string, as in 'It's'The string ends at the apostropheUse double quotes or a template literal, or write \'
Escaping a quote that doesn't need itNothing breaks, but the code is harder to readPick the outer quotes that need no escaping

Troubleshooting​

SymptomLikely causeFix
SyntaxError: Unexpected identifier pointing inside a stringA quote of the same kind as the outer ones ended the stringChange the outer quotes or escape the inner one
A backslash shows up in the outputIt was doubled, \\, which means a real backslashUse a single backslash before the quote

FAQ​

Should I use single or double quotes?​

Either. MDN says they're "treated identically". Pick one for your project and switch only when the text contains that quote.

Is a template literal slower or different?​

For plain text it gives the same string. It also lets you insert values with ${...}, which the other two don't.

Conclusion​

To get double quotes into a JavaScript string, wrap it in single quotes or a template literal, or escape each quote with a backslash. The same goes the other way for apostrophes.

Additional Resources​