5 ms·
I was in the same position a while back, and found Eloquent JavaScript [0] to be a really good read. It's quite well structured, which makes diving into the bit
by jsty 8y ago
I was in the same position a while back, and found Eloquent JavaScript [0] to be a really good read. It's quite well structured, which makes diving into the bits that sound unfamiliar a lot easier.
[0] https://eloquentjavascript.net/ https://eloquentjavascript.net/
- charliepark 8y ago(Wrote practically the same message at the same time, then deleted mine. Agreeing with the parent comment: Eloquent Javascript is really solid.)
- arethuza 8y agoI skimmed this (which looks good) and the Airbnb guide recommended (recommended in another comment) and I can't help noticing that one uses double quotes for strings and the other single quotes (Airbnb). Is there actually any sensible argument one way or the other - that isn't just being consistent with your team/project/platform...?
- deleted 8y ago[deleted]
- Klathmon 8y agoLike most lint rules, it's mostly preference. The important part is being consistent within your team/project like you said.
- __oh_es 8y agoThe reason I’ve used (and stuck to) single over double is it’s easier to embed JavaScript within html (html convention being double). It can get cluttered when with nested quotes, but most linters will allow you to use the other type to encapsulate. IMO it doesn’t matter, as long as the team agrees and remains consistent throughout the codebase.
- arethuza 8y agoThanks - that's a decent explanation for favouring one over the other. I rather dislike "best practices" that don't provide any explanation of what they are recommending.
- jrockway 8y agoI mean, it's because it doesn't matter. Picking one is valuable for consistency (no odd diffs when refactoring). Which one you pick doesn't matter.
- chrisweekly 8y ago... Unless you would prefer to skip the extra keypress required for ".
- ficklepickle 8y agoThe behavior is slightly different. "\n" is a newline, '\n' is a backslash and an n.
- lauritzsh 8y agoNot in JavaScript. \n is treated as a newline in both cases. Try it out in the console.
- khalilravanna 8y agoFor these sorts of style rules we've started using Prettier (https://prettier.io/ https://prettier.io/) to auto-format code for style. We set VS Code to just run it every time you save a file. It's been a huge win for us so developers don't have to spend time on trivial stuff like double vs single quotes and the like. No more having to comment on style inconsistency in PRs, we can focus on actual code quality. And no more arguments about what's the right style rules.
- ergothus 8y agoPrettier doesn't resolve my primary issue with excessive linting rules. The way I see it, there are three kinds of syntax linting rules: 1) Potential bugs. These are where a syntax will technically work, but makes it easy for an unintended bug to creep in. Most everyone agrees that these are bad. (There are a few arguments about what qualifies, but this is nonetheless a less-argued-about category. 2) Purely stylistic. This is where Prettier shines, because everyone wants the "more readable" option - where "readable" means "more familiar". Prettier allows you to write your preferred style, though of course anything you READ will be in the defined style (absent two-way conversion). 3) Expressiveness. This is where you're formatting your code to communicate. While Perl is notorious for this - the advocates love it because well-written code is easy to follow and maintain and the detractors hate it because just because you CAN write clearly in it, the skill to do so is rarely developed and the result is near-gibberish, even from skilled coders - like a story that has every sentence written by a different person. Java is the reverse - the advocates love it because the code tends to be very uniform and the detractors hate it because it's universally verbose and unclear. That last category is why I'm leery of excessive linting. If I'm dealing with code that changes hands often, a Java style makes sense - newspapers written for a low but common reading level, even if the reader is capable of much more nuance. In this scenario nuance would risk being ignored, or worse, being mis-applied, which means the result would be misleading. If I'm dealing with code that is owned by a team that has routine staffing changes but no complete upsets, nuance has a real benefit - reading code is faster and more accurate/informative, with the result of fewer bugs (citation needed). But saying "It's okay to use Prettier and stop arguing about linting" dismisses the nuanced case. It carries the assumption that linting rules - any rules - are automatically valuable regardless of the choices made, and if you don't like it than write what you like and just use Prettier. Linting rules (and linting arguments) exist for a reason - these are LANGUAGES that we are using to communicate - and as much as I'm tired of the arguments too, until we honestly figure out how to communicate well, ignoring the arguments doesn't help.
- z3t4 8y agoAn argument for " is you don't have to escape ' inside string's. In an argument about single vs double quote you can bring up `Template strings` and everyone will hate you ;)
- ergothus 8y agoThe sane argument is about avoiding escaping of nested quote characters. It's easier to use double quotes if your text is english (or other language that uses apostrophes). It's easier to use single quotes if your text is HTML (for double quotes) I've seen an argument to use template literals (backticks) almost everywhere. I don't know if that involves any real performance hit, but I've also not adopted this convention, at least not yet.
- metamet 8y agoTemplate literals also have the benefit of being able to embed variables without concating the string, ie: `My name is ${name}.`
- madeuptempacct 8y ago"onclick = saySomething('thethingtosay')" Single quotes for js, double quotes for html. The above is the reason.
- tokyodude 8y agoThat whole issue is arguably old JavaScript. It was because HTML style preferred double quotes so using since quotes in JS made that easier. Modern Javascript has template literals using backticks so arguably the new rule should be used double quote everywhere except use a template literals for HTML or whenever doing string manipulation. The number one thing I hate about using single quotes it is matches almost no other languages so it ruins muscle memory if switching between languages. JS itself prefers double quotes. See JSON.stringify or log anything to the console that gets auto quoted, always uses double quotes
- marcosdumay 8y agoIt matches SQL, that is the one language one is most likely to write at the same time as any other language.
- kiriakasis 8y agoThe fact that JSON.stringify uses double quotes is slightly in favour of using single quotes in handwritten code
- chrisweekly 8y agoI agree w/ other commenters that using a formatter like Prettier is a good idea. But to answer your question, single quotes don't require use of the Shift key so are a better choice for DX / ergonomics. RSIs are no joke.
- LocalPCGuy 8y agoFor a slightly different reason than many I've seen here - double-quotes require the shift key to be depressed on standard keyboards. So programming with double-quotes doubles the number of keystrokes you need to type any time you quote something. Pretty sure the person that told me that was a Vim fanatic as well. Not saying that it's a strong reason to use one over the other, but it is an objective reason that doesn't rely on style or preference.
- mr_toad 8y agoI don’t ever want to work with someone who insists on one style of quote marks in a language where it doesn’t make any difference. There’s a difference between promoting readable code and just being retentive.