Skip to content

Instantly share code, notes, and snippets.

@caub
Last active August 24, 2020 20:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caub/1bd0951ea10a309d5a4f960908cf2dd8 to your computer and use it in GitHub Desktop.
Save caub/1bd0951ea10a309d5a4f960908cf2dd8 to your computer and use it in GitHub Desktop.

Double vs Single quotes for JavaScript Strings

"foo"

  • Newcomers will already be familiar with double quotes from their language. In English, we must use double quotes " to identify a passage of quoted text. If we were to use a single quote ', the reader may misinterpret it as a contraction. The other meaning of a passage of text surrounded by the ' indicates the 'colloquial' meaning. It makes sense to stay consistent with pre-existing languages, and this may likely ease the learning and interpretation of code.

  • Double quotes eliminate the need to escape apostrophes (as in contraptions). Consider the string: "I'm going to the mall", vs. the otherwise escaped version: 'I'm going to the mall'.

  • Double quotes mean a string. When you learn a new language like Java, Python, or C, double quotes are always used. This is because, as mentioned above, double quotes have always been used in language to indicate quoted passages of text. Old books will use double quotes, while newer ones may not. It may be desirable to stick to convention in certain cases.

  • JSON notation is written with double quotes.

  • Chrome, Firefox, Edge use double quotes in their consoles

'foo'

  • Newcomers from php and ruby and shell scripting are familiar to using single quoted strings, as double-quotes strings are subject to interpolation

  • Single quotes have less visual clutter/noise for short strings

  • Qwerty keyboards need a shift for double quotes, not for singles, that's not my case with a French azerty

  • NodeJS uses single quotes

  • Airbnb style guide advices singles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment