Developing on Staxmanade

Should I Use JavaScript Single (') or Double (") Quotes?

(Comments)

The topic of using JavaScript single ' or double " quotes comes up often enough that I thought I'd create a bit of an amalgamation of items around the topic for future reference.

Today, it came up at work when someone sent a PR to our internal standard eslint config repository and there were some various points brought up with no real final solid winner or resolution.

In this post, I'm not (sadly) going to write anything interesting or profound or even original. Just wanted to aggregate some pros/cons as I've seen splattered in various locations around the web on the topic.

The most important point or the TL;DR

  • Define one standard and stick to it. Do not use one in one file, and a different one in another file, or even especially avoid mixing quotes within the same file if possible.

Basic qualities they both share

  • They both represent strings (yay! strings!)
  • Which ever one you start with, must be the same one you end the string with.
  • There is really no difference in the end between using single or double quotes, meaning they both represent a string in the end. The system doesn't really care which one you use (but you might?).
  • No need to escape the other character within a string. So a double quoted string can have single quotes without escaping them, and a single quoted string can have double quotes within it without having to escape them.
    • "Double quotes with a single ( ' ) quote in the middle"
    • 'Single quotes with a double ( " ) quote in the middle'
  • Each type must escape their own type
    • "double quotes ( \" ) must escape a double quote"
    • 'single quotes ( \' ) must escape a single quote'
  • 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 it's apparently the other way around (need <Shift> for single quote vs double).

Pros for single quotes:

  • One popular argument for single quotes is when you have to write html within javascript:
    • If you use single quotes you can write var html = '<div id="some_div"></div>'
    • If you use double quotes you must escape each nested " EX: var html = "<div id=\"some_div\"></div>" which can get annoying. Or you could use single quotes within the html string, but I don't want to cover quotes for html here (that would just hurt)...
  • Single quotes can often look better when you're using them to represent an empty string '' vs "" (too many little marks in the latter and can be difficult read - ahhh)

Cons for single quotes:

Only real con I can come up with is copying/pasting between JSON and JavaScript files - single quotes are not supported within JSON files, so you'd have to do a host of search/replace (and escaping of double quotes)...

Pros for double quotes:

  • JSON only allows double quotes (unless you control the data, and can use a library that is more liberal at parsing JSON - but a JSON file with single quotes is not truly a JSON file)
  • Most likely noobies to JS will be familiar with double quotes from their previous programming languages
  • Double quotes eliminate the need to escape apostrophes when writing english sentences.

Cons for Double quotes:

  • Must press an extra key <Shift> when wanting to use double quotes

In the end

I recommend single quotes as a solid standard. Unless you are copying JSON objects in JavaScript and pasting them into JS files a ton - it's generally my personal preference.

Some References:

Happy Quoting!

Comments