DEV Community

tutorials-kept-simple
tutorials-kept-simple

Posted on • Originally published at flexiple.com

Single vs Double Quotes 'Single' vs "Double" quotes for strings in javascript

Often while coding using javascript, you would have come across the use of 'single' or "double" quotes for strings and would have wondered, if there is any real difference between the two and if there is, is there an advantage of using one type of quote over the other? This article is going to answer just that! Read on!

Table of Contents

Difference between the two quoting styles

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

'apple' === "apple"
Enter fullscreen mode Exit fullscreen mode

Some of the other key points that both styles of quoting are as follows:

  • Whichever quoting style you open a string with, close it with the same style.
'apple' //correct
"apple" //correct
"apple' //incorrect
Enter fullscreen mode Exit fullscreen mode
  • The system doesn't care which one you use.
  • On German, Hungarian, Austrian, and many other keyboards, you have to use the Shift key for both single or double-quotes.
  • On Turkish Q keyboards, we need to press Shift for a single quote and not for a double quote!

Choosing the right quoting style

Wise selection of quoting can help you from escaping single (') or double(") quotes within a string. For example, if you wish to store a HTML snippet in a variable, you can use double quotes (") for HTML attribute values and use single quotes (') for enclosing the JavaScript string:

var div = '<div class="panel">...</div>'
Enter fullscreen mode Exit fullscreen mode

Quote within a quote

Using quotations within a string gives rise to an error. for example,

var message='Javascript's beauty is simplicity';

Enter fullscreen mode Exit fullscreen mode

There is no way for the browser to know which one is the closing quote. The interpreter sees the second quote in 'Javascript's as the ending quote - so the rest of the line becomes invalid.

We can fix this by using the fact that javascript allows both single and double quotes to define a string. So in this case you can go for double-quotes.

var message="Javascript's beauty is simplicity";

Enter fullscreen mode Exit fullscreen mode

An alternate method is to escape quote arks using a forward slash "\". You use a forward slash in front of the character you intend to escape. So the same message becomes:

var message='Javascript\'s beauty is simplicity';
Enter fullscreen mode Exit fullscreen mode

Points to remember

  • A double-quoted string can have single quotes without escaping them, conversely, a single-quoted string can have double quotes within it without having to escape them.

  • Double quotes ( \" ) must escape a double quote and vice versa single quotes ( \' ) must escape a single quote.

Single vs Double Quotes - Pros and Cons

Pros

                      Single Quotes                                   Double Quotes
 Better readability for empty strings (' ') looks   better than ("" "")  In JSON the only quoting style allowed is double quotes (" ")
 Easier if you wish to write html within javascript  Eliminates the need to escape apostrophes when writing   sentences in english

Cons

    Single Quotes                                   Double Quotes
  Not supported by JSON   Must press an extra key (Shift)  when wanting to use double quotes

Popular Quoting Style

Combing through a few popular JavaScript projects we can see a clear preference for single quotes over double-quotes.

 Project  Dominant quote     style
 lodash   ' - 99% of quotes
 chalk   ' - 100% of quotes
 react   ' - 90% of quotes
 request   ' - 97% of quotes
 commander.js   ' - 97% of quotes
 moment   ' - 90% of quotes
 express   ' - 92% of quotes
 tslib   " - 100% of quotes
 debug   ' - 97% of quotes
 node-fs-extra   ' - 98% of quotes
 axios   ' - 81% of quotes

Data obtained from https://bytearcher.com/

However, a considerable number of front-end libraries prefer double quote style which might have to do with the presence of HTML fragments.

Parting words

To sum it up, try to stick with one quoting style throughout. If you are confused about which one to pick, go with the widely-used single quotes. In ES6, you also have a third option to enclose strings - the backtick string.

Top comments (2)

Collapse
 
taufik_nurrohman profile image
Taufik Nurrohman • Edited

I think this is just a personal preference. No?

Everyone has their own style. As long as they are consistent, then that would be fine. Would like to see some style guide about it that has been used by people around the world.

To me, I will use single-quote string by default. If it has a potential to have only single-quote character in it, then I would use double-quote so that I can remove the escape character.

I use double-quote to represent empty string because it feels more assertive. As to type it requires me to press the Shift key.

Example:

let element = '<a href="#">aaa</a>',
    hash = window.location.hash;
if ("" === hash || 0 === hash.indexOf("'")) {
    window.location.hash = '#aaa';
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
drarig29 profile image
Corentin Girard • Edited

Second time I see someone using Yoda comparison 😃
eslint.org/docs/2.0.0/rules/yoda

I don't use it personally, it's just funny to me.