Switch JavaScript strings between single and double quotes

Paste JavaScript, choose single or double quotes, and every ordinary string literal is re-quoted with its escapes adjusted, while comments, regexes and backtick templates stay exactly as they were.

Live demo · real result from this tool
Quotes

Normalised code

const a = 'hello';const b = 'it\'s';const c = 'say "hi"';// "comments" are untouchedconst re = /"x"/;
Strings re-quoted2

Processed on your device — nothing is uploaded.

How to use the JS quote normalizer

  1. Paste your code into the JavaScript box.
  2. Pick Quotes: Single ’ (the default) or Double “.
  3. Keep Keep the original quote when switching would need extra escapes ticked to avoid strings full of backslashes.
  4. Copy the Normalised code and check Strings re-quoted.

What it does and when to use it

Mixed quote styles happen when code is copied from several sources or written by several people. Linters such as ESLint’s quotes rule then report dozens of warnings. This tool fixes them in one go for a pasted file or snippet, without the risk of a find-and-replace that also changes quotes inside comments, regexes or other strings.

In a project, a formatter keeps quotes consistent on every save; the JavaScript formatter has a quote option too. To turn backtick templates into plain strings first, use the template literal converter. To escape a single piece of text for a string by hand, the string escaper handles that.

How it works

The lexer splits the code into tokens and only looks at single- and double-quoted string tokens. For each one in the other style, it:

  1. Drops the backslash from escaped old quotes, so \' becomes ' when moving to double quotes.
  2. Adds a backslash before any new quote character in the text, so "say 'hi'" becomes 'say \'hi\''.
  3. Keeps every other escape, such as \n or é, as written.

With the keep option on, a string that contains the new quote character but not the old one is left exactly as it is.

Worked examples

The default sample, to single quotes.

const a = "hello";
const b = 'it\'s';
const c = "say \"hi\"";
// "comments" are untouched
const re = /"x"/;

becomes:

const a = 'hello';
const b = 'it\'s';
const c = 'say "hi"';
// "comments" are untouched
const re = /"x"/;

Two strings were re-quoted. Line c lost its backslashes, because double quotes no longer need escaping inside single quotes. The comment and the regex did not change.

Keeping an apostrophe readable. const c = "it's"; with the keep box ticked stays the same, and Strings re-quoted is 0.

Forcing the switch. The same line with the box unticked becomes const c = 'it\'s';.

Limits and tips

  • Strings only. Object keys written without quotes and template literals are not changed.
  • Run a linter afterwards if your style guide has extra rules, such as allowing the other quote to avoid escapes.
  • Directives such as "use strict" are strings too and will be re-quoted; both forms work the same.

Frequently asked questions

Why was one of my strings not changed?
With "Keep the original quote when switching would need extra escapes" ticked, a string such as "it's" stays in double quotes, because single quotes would force you to write it\'s. Untick the box to convert every string regardless.
Are template literals converted?
No. Backtick strings can hold ${…} expressions and real line breaks, so they are left alone. Use the template literal to concat tool if you want them as ordinary strings.
Does it change JSON?
It is meant for JavaScript. JSON only allows double quotes, so converting a JSON file to single quotes would make it invalid. Use the JSON formatter for JSON files.
Will it change quotes inside JSX attributes?
The lexer reads JavaScript tokens, not JSX, so attribute values such as className="box" may be treated as strings and re-quoted. Check JSX files carefully, or use Prettier with its jsxSingleQuote option.

Written by the ToolsRift team · Last updated

Pages are drafted with AI assistance and checked by automated tests. How we write and check pages.

Was this tool useful?

Report a problem

Start typing to search every tool.

↑ ↓ to moveEnter to openEsc to close