35

I recently started seeing JSLint giving the following warning:

JSLint: Expected '"' and instead saw '''.

In other words, it's wanting me to use double quotes instead of single quotes. Why is JSLint warning me about that? Aren't single quotes and double quotes more or less interchangeable in JavaScript, subject only to the programmer's preference?

Thunderforge
  • 19,637
  • 18
  • 83
  • 130

2 Answers2

38

The 2016-02-07 version of JSLint changed to prefer double quotes over single quotes. Douglas Crockford, developer of JSLint and influential developer for JavaScript and JSON, provided this rationale:

When I first met JavaScript, it [sic] was surprised that it had the two kinds of quotes, and I tried to make sense of it, using single for internal text, and double for external.

But eventually I realized that distinction isn't worth the clutter and confusion that comes from having two when only one is needed. I decided to go with double because that is what JSON uses, and it avoids errors caused by the overloading of apostrophe. I have been bitten by that.

In general, I am looking for ways to make the language smaller and better. Quotes fall in the same class as null & undefined. We really don't need both.

I tried it out on some of my own code, and I think it is an improvement. Eventually, I may add option.single to JSLint.

This did happen: option.single was added in the 2016-06-09 version of JSLint, so you can now tell JSLint to optionally ignore single quotes.

Crockford more succinctly reiterated his rationale for double quotes over single quotes in a later discussion:

I found that people had some difficulty managing the two types of quotes. Since the second set is completely unnecessary, and since the second set [single quotes] can introduce problems and confusions, I now recommend using double quotes only.

Community
  • 1
  • 1
Thunderforge
  • 19,637
  • 18
  • 83
  • 130
  • 5
    Do you know what problems with the single quote Crockford was referring to? – Dustin Hodges Aug 30 '16 at 19:58
  • 2
    @DustinHodges I see that someone asked about his "overloading the apostrophe" comment in the first quote and he gave the example of `alert( 'Don't try this!');`. I think that what he means is that an apostrophe is used both for the correct spelling of an English-language word and a syntax marker in JavaScript. Thus far, I haven't found anything about other problems he might be alluding to. – Thunderforge Aug 30 '16 at 20:20
  • 1
    thanks that's what I figured but was hoping there was something else. You face the same problem if you need a double quote in a string literal: e.g. alert("Crawford says, \"I now recommend using double quotes only.\"");. – Dustin Hodges Aug 30 '16 at 20:32
  • 2
    @DustinHodges True, but there aren't any English-language words that have quotes in them. Sentences yes, but not words. – Thunderforge Aug 30 '16 at 20:35
  • 4
    Indeed. I can see why he chose to use double quotes only, but you are only trading one problem for an equivalent problem with perhaps a lower occurrence rate. I'm just bothered by Crockford saying the single quotes are unnecessary without further explanation when I could make the same argument that double quotes are unnecessary. – Dustin Hodges Aug 30 '16 at 20:42
  • @DustinHodges so were many other people, hence jshint (which had similar rules but allowed you to configure them) and later eslint (which has many more rules and allows configuration). Many of the rules in jslint weren't entirely bad, but reflected one person's preference rather than a consistent or practical style guide. – ssube Aug 30 '16 at 20:49
  • @ssube, I hear you. I used jslint once, when it spit out several hundred errors from my code base regarding this issue and no semi-colons on the end of lines; errors I couldn't configure away. I then moved to jshint and have been happy ever since – Dustin Hodges Aug 30 '16 at 20:57
  • 9
    @DustinHodges - Mr Crockford is a smart guy, but three-quarters of his recommendations boil down to "because I said so", and he tends to explain everything as if it is all black and white even though he's just giving his opinion - and over the years he has changed his mind about this and several other issues. – nnnnnn Aug 30 '16 at 21:13
  • @nnnnnn May I know your opinion on whether his changed mind on those issues are good or something else? What else style guide do you recommend? I'm currently using JSLint, but wondering if it's that good why there are so little adoption I can find in the open source world. I'm not trying to follow the trend, just want to find the best way to write Javascript. – LiweiZ May 06 '17 at 02:54
  • 1
    Actually there are numerous, very good reasons not to use double quotes in JavaScript. A few include: 1. String literals using single or double quotes do not allow for variable expansion. This behavior is consistent with **single** quotes in other popular languages. 2. It is a pain to escape double quotes when creating HTML literal strings for templates. As of today I'm moving to ESLint. – Michael Mikowski Sep 17 '17 at 22:31
  • The problem with the higher occurrence of ' in English sentences can be mitigated two ways. One is discussed. The other is: use the typographically correct apostrophe ’ also known as U+2019. – Robert Siemer Feb 16 '20 at 16:57
14

This is a spam warning from a very opinionated tool, there's no good reason for it.

The old lint tools (JSLint and JSHint) have been wholly superseded by ESLint, which supports more rules and allows you configure the options you're actually interested in. Specifically, the quotes rule allows you to select single, double, or ignore quoting entirely.

ssube
  • 47,010
  • 7
  • 103
  • 140
  • 6
    This doesn't actually answer the question of why *JSLint* is throwing this warning. – Thunderforge Aug 30 '16 at 20:15
  • 9
    @Thunderforge It does: "This is a spam warning from a very opinionated tool." There's no good reason for this, it is purely preference on the part of a very old tool and its author. – ssube Aug 30 '16 at 20:16
  • 3
    @Thunderforge: Your answer quotes Crock(ford) as saying *"...I decided to go with double because that is what JSON uses, and it avoids errors caused by the overloading of apostrophe."* I think that squarely backs ssube's assertion that *"This is a spam warning from a very opinionated tool, there's no good reason for it."* –  Aug 30 '16 at 20:22
  • 4
    -(int)PI/3: this is a spam answer from a very opinionated user, there's no good reason for it. seriously, how is saying that "old tools" has been "wholly superseded" is not opinionated and spammy? –  Jul 09 '17 at 22:46