When to Use Double or Single Quotes in JavaScript
Almost all JavaScript developers come across the issue: when to use double or single quotes. Here, we explore possible cases, offering rational solutions.
In JavaScript, single (‘ ’) and double (“ ”) quotes are frequently used for creating a string literal.
Generally, there is no difference between using double or single quotes, as both of them represent a string in the end.
There is only one difference in the usage of single and double quotes, and it comes down to what quote character you need to escape using the backslash character (\): \’ or \”. Each type of quote should escape its own type.
For instance:
javascript quotes
"double quotes ( \" ) should escape a double quote"
'single quotes ( \' ) should escape a single quote'But there is no need to escape the other character inside a string. Hence, a double quote can have single quotes without escaping them, and vice versa.
An important argument for single quotes is when you need to write html inside JavaScript:
- When you use single quotes you can act as follows:
Javascript single quotes
- But if you use double quotes, you need to escape every "
<kbd class="highlighted">EX</kbd>:
Javascript double quotes
which can be pretty annoying.
- Single quotes look better when you use them for representing an empty string ''
<kbd class="highlighted">vs</kbd>"" .
The only disadvantage of single quotes that you may come up with is copying and pasting between JSON and JavaScript files: single quotes are not supported within JSON files.
On the contrary, JSON allows using double quotes only. Also, using double quotes, you get rid of apostrophes while writing English letters.
There is also an alternative and efficient solution: using backticks ( ). Using this type of quote has many advantages.
First and foremost, they allow simple string interpolation (“variable interpolation”). Here is an example:
javascript template literal interpolation
"string " + variable becomes `string ${variable}`Secondly, there is no need to escape (\) single or double quotes. Take a look at this example:
javascript template literal interpolation
"\"Welcome to W3Docs!\""
becomes `"Welcome to W3Docs"`Thirdly, they allow multi-line code without new line character (\n):
javascript multi line code
"Welcome to\nW3Docs!"
becomes
`Welcome to W3Docs`Also, they operate better for HTML; hence you can use them by default, as follows:
Javascript multi line code for HTML
To avoid making a choice every time you intend to write a string, we recommend you pick one of the styles described above and stick with it. Single quotes are the most common ones in contemporary programming. But, always keep in mind that JSON does not support them.
Strings and Quotes in JavaScript
The strings in JavaScript are generally used to store and manipulate text. UTF-16 is their persistent internal format. Strings are capable of representing zero or more characters, written inside quotes.
There are three ways to write strings: you can write them inside single quotes, double quotes, or backticks. The quote type used must match on both sides, though you can mix them within the same script. Single and double quotes are functionally identical. The third method, template literals, uses backticks and supports features like interpolation and multi-line formatting, working similarly to regular strings.