Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistent quote style for both JS and JSX? #629

Closed
lencioni opened this issue Dec 21, 2015 · 8 comments
Closed

Consistent quote style for both JS and JSX? #629

lencioni opened this issue Dec 21, 2015 · 8 comments
Labels

Comments

@lencioni
Copy link
Member

The style guide says to prefer single quotes for JS. However, for JSX, it prefers double quotes because:

double quotes make conjunctions like "don't" easier to type

Although in practice it probably occurs less frequently, you could make the same argument for using single quotes to make quoted bits like 'Say "hello"' easier to type.

Still, all of this seems to me to matter less than the consistency that would be gained by choosing either single quotes or double quotes for both JS and JSX. What do you think?

@ljharb
Copy link
Collaborator

ljharb commented Dec 21, 2015

The "easier to type" thing I don't really buy because one shouldn't be using straight quotes from a typography standpoint - ie, 'don’t' and "don’t" are equally easy to type. We should improve the rationale in that section.

Personally I prefer double quotes in JSX because double quotes are also ideal for HTML (even though single quotes work, they look ugly in HTML), and I see JSX and HTML as largely similar.

JS and JSX aren't the same - I don't see any value in necessarily having consistency between them. I also think that there's value in differentiating inline JSX values from JS values.

@lencioni
Copy link
Member Author

The "easier to type" thing I don't really buy because one shouldn't be using straight quotes from a typography standpoint - ie, 'don’t' and "don’t" are equally easy to type. We should improve the rationale in that section.

Agreed. The stated rationale is pretty weak.

double quotes are also ideal for HTML (even though single quotes work, they look ugly in HTML)

Is this just aesthetics or do you see other reasons why double quotes are ideal in HTML?

JS and JSX aren't the same - I don't see any value in necessarily having consistency between them.

Since JSX is a layer of syntactic sugar on top of JS, they seem pretty similar to me--at least in terms of what they do and how they operate. I think the value in having consistency between them in the areas they overlap is that when writing code, developers won't have to think about which style they should be using. This is particularly pronounced when you have JS that takes string arguments in your JSX props next to other string props.

<MyComponent
  someText="Hello"
  somethingElse={this.makeSomething('good')}
/>

It's not a huge deal, but I think that these microdecisions add up, and if it is easy to minimize them, then we should.

I also think that there's value in differentiating inline JSX values from JS values.

Perhaps, but what would this value be?

@ljharb
Copy link
Collaborator

ljharb commented Dec 21, 2015

Different rules apply. JS expressions can have side effects, JSX inline strings can't. (This is a weak argument in favor of the difference, but I'm just pointing it out).

I understand the majority viewpoint that "JSX is not HTML, it's sugar over JS", but that's just not how I see it :-)

@AlicanC
Copy link
Contributor

AlicanC commented Dec 28, 2015

I understand the majority viewpoint that "JSX is not HTML, it's sugar over JS", but that's just not how I see it :-)

When I saw JSX I was like "HTML in JS? Booyah!" until this exploded in my face:

return (
  <div class="someclass" />
);

I started using single quotes for JSX because it immediately sets my brain's "This is not HTML, this is JSX." mode on. HTML has no power here. It's all about it's JS API.

Edit: Fixed the typo mentioned in the following comments.

@ljharb
Copy link
Collaborator

ljharb commented Dec 28, 2015

well, if you write HTML in the XHTML style, you'd never not close a non-self-closing tag :-p

@AlicanC
Copy link
Contributor

AlicanC commented Dec 28, 2015

Oh no, that was just a typo. The real catch is that you use "className" and not "class".

@goatslacker
Copy link
Collaborator

Was also discussed here #269 (comment)

@ljharb
Copy link
Collaborator

ljharb commented Jan 7, 2016

Closing due to the discussion in that PR.

@ljharb ljharb closed this as completed Jan 7, 2016
KevinGrandon added a commit to KevinGrandon/javascript that referenced this issue Nov 1, 2016
ramasilveyra pushed a commit to auth0/javascript that referenced this issue Dec 7, 2016
hibearpanda pushed a commit to 15Prospects/javascript that referenced this issue Jan 22, 2017
jaylaw81 pushed a commit to appirio-digital/ads-best-practices that referenced this issue Sep 19, 2017
sensiblegame added a commit to sensiblegame/React-BNB that referenced this issue Oct 23, 2017
passionSeven added a commit to passionSeven/javascript that referenced this issue Jan 27, 2023
Binary-Ninja-007 pushed a commit to Binary-Ninja-007/JavaScript_Style_Guide that referenced this issue Aug 13, 2023
harry908nilson pushed a commit to marcolane/Javascriipt that referenced this issue Sep 1, 2023
olehrab pushed a commit to olehrab/JavaScript that referenced this issue Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants