Categories
conventions javascript string

When should I use double or single quotes in JavaScript?

2209

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

I see more and more JavaScript libraries out there using single quotes when handling strings. What are the reasons to use one over the other?

I thought they’re pretty much interchangeable.

13

  • 146

    which is easier to read? alert(“It’s game time”); or alert(‘It\’s game time’);

    Jun 9, 2011 at 16:10


  • 665

    How about this Ryan? alert("It's \"game\" time."); or alert('It\'s "game" time.');?

    – Francisc

    Oct 11, 2011 at 18:37


  • 44

    If single quotes always used and occasionally double quotes where the literal contains single quote, then we will have to type far less shift buttons and our left little finger will give us blessings. But yes, as @arne said, for JSON double quote should be used.

    – IsmailS

    Oct 31, 2011 at 15:46

  • 12

    Single quoting is easier to do when you’re on a european keyboard (double quote is Shift+2 which isn’t as sweet as tapping a single key conveniently by your right pinky).

    – Arne

    Apr 25, 2012 at 20:46

  • 42

    @Arne There is no such a thing as an “European keyboard”. E.g. the German keyboard requires shift for both types of quotes. (But single quotes are easier.)

    – ANeves

    Jun 18, 2012 at 15:53

1381

The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.

Using the other type of quote as a literal:

alert('Say "Hello"');
alert("Say 'Hello'");

This can get complicated:

alert("It's \"game\" time.");
alert('It\'s "game" time.');

Another option, new in ECMAScript 6, is template literals which use the backtick character:

alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);

Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.

Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.

14

  • 105

    An important point to note with all code conventions – Define it once and stick with it. IOW, don’t use double quotes someplace and single quotes elsewhere.

    – Cerebrus

    May 2, 2009 at 8:03

  • 203

    @Cerebrus – I think flexibility is OK with this one. Sure pick a preferred style, but if you need to break away from the style to save escaping lots of quotes in one string. I’d be OK with that.

    May 2, 2009 at 8:11

  • 12

    I don’t think there’s any reason to have to be consistent about it. There’s no advantage to either one, and I don’t think the readability is really affected whether or not you use ‘ in one place and ” in another.

    – cdmckay

    May 2, 2009 at 8:14

  • 3

    @Olly Hicks, interesting test!! Single quotes are actually several times faster than double quotes here in Chrome 13 (OSX). Interesting…

    – Ricket

    Sep 14, 2011 at 23:48

  • 19

    A little off-topic, but if people used correct typography, a lot of these discussions about escaping would be obsolete: alert('It’s “game” time') vs. alert("It’s “game” time") – doesn’t matter. You would only need to escape in the (rare) cases where the single or double prime sign ', " is actually appropriate.

    – jotaen

    Feb 4, 2018 at 11:26


664

If you’re dealing with JSON, it should be noted that strictly speaking, JSON strings must be double quoted. Sure, many libraries support single quotes as well, but I had great problems in one of my projects before realizing that single quoting a string is in fact not according to JSON standards.

14

  • 7

    This is very relevant when working with jQuery.ajax calling into an ASP.NET service (Web Service, Page Method, or MVC).

    – Schmuli

    Mar 3, 2011 at 11:46

  • 111

    The property names within the JSON strings must be double-quoted, but a JSON string as a whole can be single-quoted: var jsonString = '{"key1":"value1"}'; (Not that I recommend manually building JSON.)

    – nnnnnn

    Feb 9, 2012 at 10:45


  • 54

    You shouldn’t write JSON by hand if you can .stringify() it.

    Feb 3, 2013 at 2:09

  • 26

    This right here is the best argument for always using double quotes. JSON should have double-quotes. The other answers are mostly giving the advice to “be consistent”, which means if any one part of the language might realistically force a double quote, you should consistently use that double quote.

    Jul 14, 2014 at 16:07

  • 19

    This is also relevant when working with multiple languages where nearly all other lanuguages (Java, C, C++, …) use double quotes for strings and single quotes for chars. I prefer to use the same quoting across the board and so stick with double quotes for JS. With years of touch typing the extra key to shift for double quotes is entirely irrelevant, and if your coding is constrained by your typing then you need to practice typing properly.

    Sep 24, 2014 at 17:17

383

There is no one better solution; however, I would like to argue that double quotes may be more desirable at times:

  • Newcomers will already be familiar with double quotes from their language. In English, we must use double quotes " to identify a passage of quoted text. If we were to use a single quote ', the reader may misinterpret it as a contraction. The other meaning of a passage of text surrounded by the ' indicates the ‘colloquial’ meaning. It makes sense to stay consistent with pre-existing languages, and this may likely ease the learning and interpretation of code.
  • Double quotes eliminate the need to escape apostrophes (as in contractions). Consider the string: "I'm going to the mall", vs. the otherwise escaped version: 'I\'m going to the mall'.
  • Double quotes mean a string in many other languages. When you learn a new language like Java or C, double quotes are always used. In Ruby, PHP and Perl, single-quoted strings imply no backslash escapes while double quotes support them.

  • JSON notation is written with double quotes.

Nonetheless, as others have stated, it is most important to remain consistent.

10

  • 5

    Your first point about English language is not always true and can change depending on the locality/house convention. Printed materials typically use single-quotes for speech and use other formatting for large blocks of quoted text. Your ‘colloquial’ meaning is not a useful definition of quotes for emphasis. Plus English users are in general very poor with quotation marks and apostrophes.

    Jan 22, 2015 at 13:24

  • 3

    @JohnFerguson, for that reason alone, it may be desirable to use double-quotes so as to make that differentiation (between apostrophes and quoted passages).

    Jan 22, 2015 at 17:24

  • I’m all about pragmatism. Due to the fact that 1 in 100 strings that I type or uses has double quotes, and many, many more have apostrophes, I use doubles. At the end of the day, though, you should use the quote type that’s 1) already in use in the project if you’re a new developer to the project, or 2) use the one that you think makes more sense.

    – dudewad

    Nov 16, 2015 at 17:43


  • Case in point– what I just typed (there are multiple apostrophes, no double quotes 😉

    – dudewad

    Nov 16, 2015 at 17:44

  • To add to @user1429980’s third point, single quotes denotes a different datatype in Java and C.

    Aug 2, 2016 at 6:33