Strings must use {a}quote

When do I get this error?

The "Strings must use singlequote" and "Strings must use doublequote" errors are thrown when JSHint or ESLint encounters string literal delimited by double quote characters when the quotmark option is set to single or a string literal delimited by single quote characters when the quotmark option is set to double. In the following example we attempt to assign a string literal to the variable x:

/*jshint quotmark: double */
var x = 'My String';

Why do I get this error?

This error is raised to highlight a deviation from a specific coding style. Your code will run fine if you do not fix this error, but it demonstrates a lack of care. There is no difference in JavaScript between single and double quotes. This is made clear by the grammar for string literals (ES5 §7.8.4):

StringLiteral ::
    " DoubleStringCharactersopt "
    ' SingleStringCharactersopt '

The only difference is that DoubleStringCharacters cannot contain another double quote, and SingleStringCharacters cannot contain a single quote (as that would terminate the string literal). If this option is set to either double or single then it is likely your codebase requires you to conform to a specific style in which one type of quote is preferred. To fix the error, simply use the correct type of quote:

/*jshint quotmark: double */
var x = "My String";

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. The identifier of this warning is W108 (for double quotes) or W109 (for single quotes). This means you can tell JSHint to not issue this warning with the /*jshint -W108 */ or /*jshint -W109 */ directive.

About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.