Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why "Single-quoted string preferred over double-quoted string." in js?

While using gjslint, I got a hint: "Single-quoted string preferred over double-quoted string".

So why? I'm a little confused with this. Why single-quoted preferred?

Hope to get some help. Thanks all.

like image 554
Lynn Avatar asked Oct 01 '11 04:10

Lynn


People also ask

Why use single quotes instead of double quotes in JavaScript?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

Why does JavaScript use single quote?

In JavaScript, single (' ') and double (“ ”) quotes are frequently used for creating a string literal. Generally, there is no difference between using double or single quotes, as both of them represent a string in the end.

What is the difference between single-quoted strings and double quoted strings?

The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value.


2 Answers

It's just somebody's opinion.

Lots of people do prefer singe-quotes, but lots of other people prefer double-quotes. I tend to use double-quotes just out of habit from other languages, but I don't have a strong preference: I'm willing to change if I hear of a compelling reason why single-quotes are better, but to date I've not even heard a good reason, let alone a compelling reason.

Even the Google JavaScript Style Guide says that single-quotes are preferred without giving a good reason:

"For consistency single-quotes (') are preferred to double-quotes ("). This is helpful when creating strings that include HTML"

(It then goes on to give a very short example of a string that doesn't even include HTML.)

A lot of people seem to believe XHTML is only valid with double-quotes, but that is not true: the XHTML spec allows either. (Having said that, 98% of the XML I've run across, including XHTML, does use double-quotes.)

Within any one source file I try to stick to one or the other, but if a line of code in the middle needed a lot of embedded quotes I'd be happy to change for just that line to avoid lots of escaping in that line.

UPDATE: Just remembered that JSON is only valid with double-quotes. Obviously JSON is not JavaScript, but still that's a reason why (a) some might prefer double-quotes in their JavaScript so that object literals look like JSON, and (b) some might prefer single-quotes so that they can manually encode JSON without having to escape the double-quotes. (No, I don't recommend manually encoding JSON, but I've seen it done.)

like image 97
nnnnnn Avatar answered Sep 30 '22 20:09

nnnnnn


If you use single quotes, you do not have to escape the double quotes in your html.

For example...

With single quotes you can do this

var a = '<a href="something.html" rel="yes" title="whatever">a link/a>';

with double quotes you would need to escape those inside double quotes, like so

var a = "<a href=\"something.html\" rel=\"yes\" title=\"whatever\">a link/a>";

Much more work, more difficult to maintain, and easier to commit errors.

like image 34
Jason Gennaro Avatar answered Sep 30 '22 20:09

Jason Gennaro