Should I use 'single' or "double-quotes" for strings in JavaScript

You've seen both 'single quotes' and "double quotes" used for writing strings in JavaScript. You're wondering if they have any difference. Is there a preference over the other? Which way should you use in your code?

They are the same thing

In JavaScript, a string is a sequence characters enclosed in single or double quotes. The choice of quoting style is up to the programmer, and either style has no special semantics over the other. There is no type for a single character in JavaScript - everything is always a string.

1 'abc' === "abc"

Strategic selection of the quote character can save you from escaping ' or " characters inside the string. For example, when storing a HTML snippet in a variable, you can use " for HTML attribute values if you use ' for enclosing the JavaScript string:

1 var div = '<div class="panel">...</div>'

Remember, that in JSON, the only allowed quote character is the double quotes.

1 {
2     "in_json": "you_use_only_double_quotes"
3 }

Single quotes are more common

Checking a few source repositories of popular JavaScript projects reveals that single quotes are favored over double quotes.

Project Dominant quote style
async ' - 76% of quotes
express ' - 92% of quotes
lodash ' - 73% of quotes
request ' - 97% of quotes
underscore ' - 78% of quotes
angular.js ' - 58% of quotes
react ' - 52% of quotes
Use of quote character in popular projects.

The fact that front-end libraries have more double quotes than the other libraries might have to do with the presence of HTML fragments.

Looking at a few style guides reveals a mixed approach, about half recommending single quotes and other half double quotes.

Stick to one and keep it consistent

To avoid making a choice every time you're about to write a string, you should pick one style and stick with it. If your team is still making up your mind - pick single quotes, they are more common. In ES6, you'll have a third option to enclose strings - the `backtick` string.

Related articles

Asynchronous calls - the surest way
to get lost with Node.js
Asynchronous Call Tree
What if you could turn any spec into working code?
Share article: