Table of Contents
- Difference between the two quote styles
- Choosing the right quoting style
- Single vs Double Quotes - Pros and Cons
- Popular Quoting Styles
- Parting Words
Difference between the two quoting styles
'apple' === "apple"
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
- 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
var div = '<div class="panel">...</div>'
Quote within a quote
Using quotations within a string gives rise to an error. for example,
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:
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
|Single Quotes||Double Quotes|
|Better readability for empty strings (' ') looks better than ("" "")||In JSON the only quoting style allowed is double quotes (" ")|
|Single Quotes||Double Quotes|
|Not supported by JSON||Must press an extra key (Shift) when wanting to use double quotes|
Popular Quoting Style
|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.
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
Top comments (2)
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.
Second time I see someone using Yoda comparison 😃
I don't use it personally, it's just funny to me.