Skip to content

JavaScript Quote Usage

Spinning Idea edited this page Jan 2, 2020 · 2 revisions

JavaScript Quote Usage

Below are thoughts and reasons to use double quotes as a standard in coding JavaScript as opposed to using single quotes

Either single quotes (‘ ’) double quotes (“ ”) are used in JavaScript to create a string literal. This is key only in that it affects which quote character you have to escape using the backslash () character: \’ or \”.

Escaping

When using double quotes to create a string literal, the single quote character needs to be escaped using the backslash character: \’.

“Double quotes ‘escape’ double quotes“

const example1= "I contain 'single' quotes" const example2 = "I contain "double" quotes"

JSON

JavaScript Object Notation (JSON), the lightweight data storage format, only allows double quotes.

When passing data between JavaScript to JSON files do not use single quotes in JSON and this is one other reason to use double quotes in your coding standards and eslint rules

ESLint Rules

The eslint: quotes rule can require the use of double quotes (default), single quotes, or backticks whenever possible. The quotes rule can also enforce one type of quote except when the string contains a quote character that would have to be escaped otherwise.

https://eslint.org/docs/2.0.0/rules/quotes

Cons for double quote usage

  1. Airbnb’s style guide prefers single quotes (‘ ’), avoids double quotes (“ ”), and uses backtick literals (``) sparingly - https://github.com/airbnb/javascript#strings--quotes
Clone this wiki locally