9 JavaScript allows you to define strings in one of three ways: double quotes, single quotes, and backticks (as of ECMAScript 6). For example:
14 var double = "double";
15 var single = 'single';
16 var backtick = `backtick`; // ES6 only
19 Each of these lines creates a string and, in some cases, can be used interchangeably. The choice of how to define strings in a codebase is a stylistic one outside of template literals (which allow embedded of expressions to be interpreted).
21 Many codebases require strings to be defined in a consistent manner.
25 This rule enforces the consistent use of either backticks, double, or single quotes.
29 This rule has two options, a string option and an object option.
33 * `"double"` (default) requires the use of double quotes wherever possible
34 * `"single"` requires the use of single quotes wherever possible
35 * `"backtick"` requires the use of backticks wherever possible
39 * `"avoidEscape": true` allows strings to use single-quotes or double-quotes so long as the string contains a quote that would have to be escaped otherwise
40 * `"allowTemplateLiterals": true` allows strings to use backticks
42 **Deprecated**: The object property `avoid-escape` is deprecated; please use the object property `avoidEscape` instead.
46 Examples of **incorrect** code for this rule with the default `"double"` option:
51 /*eslint quotes: ["error", "double"]*/
53 var single = 'single';
54 var unescaped = 'a string containing "double" quotes';
55 var backtick = `back\ntick`; // you can use \n in single or double quoted strings
60 Examples of **correct** code for this rule with the default `"double"` option:
65 /*eslint quotes: ["error", "double"]*/
68 var double = "double";
70 tick`; // backticks are allowed due to newline
71 var backtick = tag`backtick`; // backticks are allowed due to tag
78 Examples of **incorrect** code for this rule with the `"single"` option:
83 /*eslint quotes: ["error", "single"]*/
85 var double = "double";
86 var unescaped = "a string containing 'single' quotes";
91 Examples of **correct** code for this rule with the `"single"` option:
96 /*eslint quotes: ["error", "single"]*/
99 var single = 'single';
100 var backtick = `back${x}tick`; // backticks are allowed due to substitution
107 Examples of **incorrect** code for this rule with the `"backtick"` option:
112 /*eslint quotes: ["error", "backtick"]*/
114 var single = 'single';
115 var double = "double";
116 var unescaped = 'a string containing `backticks`';
121 Examples of **correct** code for this rule with the `"backtick"` option:
126 /*eslint quotes: ["error", "backtick"]*/
129 var backtick = `backtick`;
136 Examples of additional **correct** code for this rule with the `"double", { "avoidEscape": true }` options:
141 /*eslint quotes: ["error", "double", { "avoidEscape": true }]*/
143 var single = 'a string containing "double" quotes';
148 Examples of additional **correct** code for this rule with the `"single", { "avoidEscape": true }` options:
153 /*eslint quotes: ["error", "single", { "avoidEscape": true }]*/
155 var double = "a string containing 'single' quotes";
160 Examples of additional **correct** code for this rule with the `"backtick", { "avoidEscape": true }` options:
165 /*eslint quotes: ["error", "backtick", { "avoidEscape": true }]*/
167 var double = "a string containing `backtick` quotes"
172 ### allowTemplateLiterals
174 Examples of additional **correct** code for this rule with the `"double", { "allowTemplateLiterals": true }` options:
179 /*eslint quotes: ["error", "double", { "allowTemplateLiterals": true }]*/
181 var double = "double";
182 var double = `double`;
187 Examples of additional **correct** code for this rule with the `"single", { "allowTemplateLiterals": true }` options:
192 /*eslint quotes: ["error", "single", { "allowTemplateLiterals": true }]*/
194 var single = 'single';
195 var single = `single`;
200 `{ "allowTemplateLiterals": false }` will not disallow the usage of all template literals. If you want to forbid any instance of template literals, use [no-restricted-syntax](no-restricted-syntax) and target the `TemplateLiteral` selector.
202 ## When Not To Use It
204 If you do not need consistency in your string styles, you can safely disable this rule.