Quotes in JavaScript
An overview of the quotes allowed in JavaScript and their unique features
JavaScript allows you to use 3 types of quotes:
- single quotes
- double quotes
- backticks
The first 2 are essentially the same:
const test = 'test'
const bike = "bike"
There’s little to no difference in using one or the other. The only difference lies in having to escape the quote character you use to delimit the string:
const test = 'test'
const test = 'te\'st'
const test = 'te"st'
const test = "te\"st"
const test = "te'st"
There are various style guides that recommend always using one style vs the other.
I personally prefer single quotes all the time, and use double quotes only in HTML.
Backticks are a recent addition to JavaScript, since they were introduced with ES6 in 2015.
They have a unique feature: they allow multiline strings.
Multiline strings are also possible using regular strings, using escape characters:
const multilineString = 'A string\non multiple lines'
Using backticks, you can avoid using an escape character:
const multilineString = `A string
on multiple lines`
Not just that. You can interpolate variables using the ${} syntax:
const multilineString = `A string
on ${1+1} lines`
I cover backticks-powered strings (called template literals) in a separate article, that dives more into the nitty-gritty details.
download all my books for free
- javascript handbook
- typescript handbook
- css handbook
- node.js handbook
- astro handbook
- html handbook
- next.js pages router handbook
- alpine.js handbook
- htmx handbook
- react handbook
- sql handbook
- git cheat sheet
- laravel handbook
- express handbook
- swift handbook
- go handbook
- php handbook
- python handbook
- cli handbook
- c handbook
subscribe to my newsletter to get them
Terms: by subscribing to the newsletter you agree the following terms and conditions and privacy policy. The aim of the newsletter is to keep you up to date about new tutorials, new book releases or courses organized by Flavio. If you wish to unsubscribe from the newsletter, you can click the unsubscribe link that's present at the bottom of each email, anytime. I will not communicate/spread/publish or otherwise give away your address. Your email address is the only personal information collected, and it's only collected for the primary purpose of keeping you informed through the newsletter. It's stored in a secure server based in the EU. You can contact Flavio by emailing flavio@flaviocopes.com. These terms and conditions are governed by the laws in force in Italy and you unconditionally submit to the jurisdiction of the courts of Italy.