Photo by Pixabay from Pexels
If there is one past time that programmers like to engage with is having discussions about formatting. It is a question of taste, a statement of your personal style and something where only one opinion can be the right answer.
Let’s take a look at the all important question if
'' is better than
First off is there any meaningful difference
Generally, there is no difference between using double or single quotes, as both of them represent a string in the end. W3docs
Pro Double Quotes
It is highly likely that you are going to have a
.json file in your project. (like the
VSCode Default Settings
For TypeScript there is the option to ‘automatically detect single or double quotes’ but in practice it still will just import them with double quotes.
Following Styleguides use doublequotes as default:
- Prettier Default Settings
- MDN Web Docs
- Github Rules for TypeScript
- TypeScript Handbook (Not explicit rule, but uses
double-quotesin all examples)
Easier to write English
When you use double quotes you do not have to escape apostrophes.
However you will have to escape quotes
const uncommonButtonLabel = 'Say: "Hello World"'
Now for single quotes, obviously you will have to escape the apostrophe. In practice, in most case you are going to dealing with a lot of strings that are using quotes.
Cons Double Quotes
More keys to press
Let’s face it, a single quote is just pressing on the keyboard the
'-key but when you want to use a double quote you need to press
shift + '-key
Pros Single Quotes
Visually more appealing
A this is highly subjective, but some developers say:
Better readability for empty strings (’ ’) looks better than ("" "") Flexiple.com
In the camp for pro
single-quote the argument is as follows, when writing an
html-string it is easier to write it with signal quotes:
const htmlString = '<a href="cool.html">Sunglasses</a>'
There are two flaws in this argument:
- In HTML both single-quotes and double-quotes are valid. There is the argument to be made that it is more ‘usual’ to use
double-quotes(Example you open the Developer Tools in Chrome, the HTML-code will be formatted with double-quotes)
- If you are creating a html-template string you should be using backticks
Thus the correct way of writing a HTML-String would be as follows:
const htmlString = `<a href="cool.html">Sunglasses</a>`
In practice, you are probably going to be using
single-quotes, because of the popular style guides like AirBnB have ‘single-quotes’ set as default.
Personally, I prefer the