Deep Tech Point
first stop in your tech adventure

Should I use single (‘) or double (“) quotes in JavaScript?

March 1, 2021 | Javascript

Should I use single (‘) or double (“) quotes when working in JavaScript? That’s the question now.

'abc' === "abc" // true

It really doesn’t matter whether you use either single (‘) or double (“) quotes in JavaScript. Decide on one standard and stick to it, for example, do not use single quotes in one file, and double in another, or particularly avoid mixing quotes within the same file if possible. Try to stick to one standard, that’s it.

So, what are the main points of single or double quotes?

At the end of the day, JavaScript really doesn’t care whether you’re using single or double quotes, however, there is a small catch. Read on. As the presentation of ‘abc’ === “abc” above shows – either quote character is identical. Except for when escaping itself. What? Wait, wait, hang in there, we’ll get there.

Each type of quote must escape its own type inside of a string

You can use double quotes with a single ( ‘ ) quote in the middle of a string, but you cannot use double quotes in the middle of a string – if you do, you’ll have to escape them. And the same goes for single quotes: you can use single quotes with double ( ” ) quotes in the middle of a string, but you cannot use single quotes in the middle of a string – if you do, you’ll have to escape them.

So, each type of quote – either single or double – must escape its own type inside of a string.

Single quotes must “escape” single quotes

When you’re using single quotes (”) to create a string literal, you have to use backslash (\’) to escape the single quote character, like so:

var escape = 'No, I won\'t do "that".';

We escaped grammatically correct won’t because we started and ended a string with single quotes, but as you can see we used double quotes inside a single-quoted string without any issues.

Double quotes must “escape” double quotes

When you’re using double quotes (” “) to create a string literal, you have to use backslash (\”) to escape the double-quote character, like so:

var escape = "No, I won't do \"that\".";

So, this time we have no issues with won’t because we started and ended a string with double quotes, but as you can see we used backslash to escape double quotes with “that”.

So, which quotes should you use in JavaScript – single or double?

You decide what is more convenient for you. Personally, I prefer single quotes, but it is really up to you what is your preference.

One popular argument for single quotes is when you have to write html within javascript you don’t have to escape them:

var single_html = '<div id="something_we_write_in_div"></div>';

But if you use double quotes, you must escape those that are nested and that can get annoying, at least to me. See what I am talking about:

var double_html = "<div id=\"something_we_write_in_div\"></div>";

And the only disadvantage I came up with regarding using single quotes is working with JSON files because JSON does not support single quotes. So, if you’re copying and pasting between JSON and JavaScript files, you will have to escape double quotes by search and replace. No big deal, but something to be aware of.

Are there any advantages to using double quotes? Absolutely. Besides JSON files that won’t give you additional work with quotes, another advantage would be when writing in English – you won’t have to escape apostrophes when using double-quotes.