44

I notice that a lot of times, even if a question is pure vanilla JavaScript, folks will answer in the context of a library like jQuery. I get that this works, but wouldn't it make more sense for answers to match and also be pure vanilla JavaScript?

I get a lot of people love their libraries, but many folks asking questions are newbies who are still trying to learn JavaScript and when an answer references a library it makes it that much more difficult for the newbie to learn.

When you barely know JavaScript, trying to decode the jQuery just makes it more complicated.

They should be learning vanilla JavaScript first and then libraries like jQuery.

Thoughts?

27
  • 82
    -1, not enough jQuery. Commented Apr 19, 2016 at 15:34
  • 2
    Huh? I do not understand... Commented Apr 19, 2016 at 15:35
  • 23
    That was a pun (and no, that downvote is not mine). See our jQuery meme. Commented Apr 19, 2016 at 15:38
  • 15
    There is a place to solve somebody's programming problem and there is a place to teach somebody how to program. SO is for problem solving, teaching is done in a school by paid professionals. Commented Apr 19, 2016 at 15:51
  • 24
    @HansPassant There's more to it than that, including a library like jQuery (plus plugins) to add a number to another number, or alternatively, suggesting libraries when OP specifically mentions that they can't (or won't) use a 3rd party library, is unacceptable. Also, the JavaScript tag excerpt says: "Unless another tag for a framework/library is also included, a pure JavaScript answer is expected."
    – Madara's Ghost Mod
    Commented Apr 19, 2016 at 15:57
  • 9
    @MadaraUchiha or adding a library to, say, pad a string? ;) Commented Apr 19, 2016 at 16:09
  • 1
    The easy solution is of course to mention in your questions whether or not you're using a library, and whether or not you're open to answers suggesting you use one. Commented Apr 19, 2016 at 16:20
  • 3
    @MadaraUchiha This isn't discussing the case where a/the library is specifically mentioned as not an option, just where it isn't explicitly stated that it is an option. And of course suggesting that a library be added when it's adding no useful tools for solving the problem at hand is a good criticism of such an answer, but not a valid criticism when an answer uses a library appropriately to solve problems it was specifically designed to solve.
    – Servy
    Commented Apr 19, 2016 at 16:32
  • 2
    @Servy I actually don't mind if you suggest a library appropriately when it solves a specific problem very well and can help overall (I suggest Bluebird to people all the time). It's the other cases I mentioned that bother me, and I do see them quite often (specifically with jQuery)
    – Madara's Ghost Mod
    Commented Apr 19, 2016 at 16:36
  • 12
    Unfortunately, many developers cannot actually write code. They survive by connecting library calls together, (whether this makes sense or not), and calling it programming. Commented Apr 19, 2016 at 17:47
  • 5
    Sadly, most developers don't know how to write vanilla JavaScript anymore. I can't tell you how many times I've looked up how to perform simple tasks and found nothing but jQuery answers.
    – cimmanon
    Commented Apr 20, 2016 at 0:53
  • 1
    @MartinJames I wouldn't blame developers for connecting library calls... Unlike few lucky ones, many developers has a boss, bosses gives very unrealistic deadlines which makes it impossible to write all the code that puts together a real world applications. Heck, some bosses are aware of the existence of these connectable library calls which makes the deadlines shorter than what is required to even connect these library calls \(^.^)\
    – T J
    Commented Apr 20, 2016 at 4:27
  • 2
    Why doesn't the OP say if they want generic JS in the answer or specify the use of no libraries? Just be more specific about the answer you're looking for.
    – Bulrush
    Commented Apr 20, 2016 at 13:40
  • 1
    @HansPassant: I do not think the two have to be mutually exclusive. You can solve a problem and teach at the same time. In fact, I would argue that is the only way to correctly solve a problem because other wise you risk creating more problems. IMHO. Commented Apr 20, 2016 at 14:50
  • 1
    @HansPassant Solving problems is very subjective and a 1 time thing. SO is for Questions and answers that benefit everyone in the long run. Giving jQuery answers devalues the question and answer as jQuery is a library, that is not necessarily compatible with other libraries. Also it introduces a tag problem, should someone using angular put the angular tag on every of his javascript questions and everyone using jQuery the jQuery tag on his question just to get a solution that works for him ? Commented Apr 20, 2016 at 20:30

6 Answers 6

53

If you feel like a given answer isn't helpful because of its use of an external library, you're certainly welcome to reflect that opinion with your vote, if you feel sufficiently strongly about it. You can also post your own answers without using such libraries if you feel that they would be more useful.

Clearly the people posting such answers disagree with your opinion and feel that those answer are more helpful, an opinion that they're certainly welcome to hold.

7
  • 3
    In an odd way I believe I've even benefited from this. A user asks how to solve a JavaScript issue (no frameworks) and gets the standard jQuery answer and or the (you should use Angular/React/ShinyFrameworkOfTheMonth)... so I'll provide the vanilla version answer. That said I see no harm in several answers for the user... providing different options to "skin a cat".
    – scunliffe
    Commented Apr 19, 2016 at 20:56
  • I agree and maybe it is good to have multiple answers but I fear one of the negative consequences is the deepening of the answer pool for an already inexperienced swimmer. How is that newbie to know which answer to go with, especially if people upvote the library one or check the answer before someone is able to reply with a vanilla answer. Commented Apr 20, 2016 at 14:56
  • 1
    @IMTheNachoMan That's making the implicit assumption that using a library is always going to be worse for the OP. That's an assumption I would reject. There will be lots of situations where it'll be better.
    – Servy
    Commented Apr 20, 2016 at 15:31
  • @Servy: I agree. I am not saying it is always worse. It, like any tool, has a time and place and best use-case. But I feel if a question reigns in newbie territory then an answer with libraries just convalutes. IMHO. Commented Apr 20, 2016 at 15:33
  • @IMTheNachoMan That's not a premise of the question. The question is referring to any question where no libraries are being used in the code in the question. Of course, that said, there will even be problems that newer developers would face that would benefit from using a library.
    – Servy
    Commented Apr 20, 2016 at 15:47
  • @Servy: I see your point. Another perspective, libraries come, go, and change often. Most of the time a solution/algorithm to use to solve a problem stands the test of time. Kinda hard to use an answer that utilizes a library that no longer exists. I realize that is highly unlikely with jQuery but jQuery functions/abilities might come/go. Commented Apr 20, 2016 at 20:21
  • 3
    @IMTheNachoMan Programming languages come, go, and change. Should we never answer questions that use programming languages because those programming languages might become obsolete, change, or function differently in a newer version?
    – Servy
    Commented Apr 20, 2016 at 20:39
12

In my opinion, answers should first and foremost solve the problem and explain what is wrong in the library (or lack thereof) used in the question. Then, and only then, should an alternative solution in another library (or no library) be provided.

8

My opinion:

If the OP mentioned that they prefer pure JS solution, then solutions should highlight pure JS answer, with optional answers using another libraries (adding information on why they should be used for this task).

Otherwise, the best possible answer should be given, in either pure JS or any commonly used, easily integrable library.

3

It's up to the asker to select as the correct answer the one that helped him solve his problem. If a jQuery answer works for them, that's fine. If the hypothetical poster doesn't get it, he can comment as such, and accept the inevitably-following vanilla answer.

Future people finding the question/answer may up/downvote as they see fit, with regard to which answers help them or not.

-14

To reverse your question, why would you not want to use a widely used library that would reduce the amount of work, and the complexity of the solution?

By using a library, like jQuery, you are being more efficient. If you are a newbie learning javascript, then part of that learning process is learning what tools and libraries are available to help you. Why reinvent the wheel?

21
  • 20
    By suggesting a library like jQuery for problems that are trivial to solve in native JS, you are promoting laziness and preventing understanding of the actual problem. I've seen programmers with "there's probably a library that does that" as first response to most issues. That's not the message we want to send.
    – Cerbrus
    Commented Apr 20, 2016 at 14:10
  • 9
    @Cerbrus Who said the problem was trivial?
    – podiluska
    Commented Apr 20, 2016 at 14:34
  • 4
    @podiluska I wager you would agree that one should first know how to walk before trying to run. Too too too many programmers don't know the basics and are trying to solve a problem with a library. Libraries are great, no denying that, but you gotta understand the core concepts IMHO. Especially when they use large libraries unnecessarily, like when a simpler vanilla answer does the trick. Commented Apr 20, 2016 at 14:59
  • 1
    @podiluska: Instead of commenting on my choice of words, how about replying to what I'm trying to say there?
    – Cerbrus
    Commented Apr 20, 2016 at 16:01
  • 3
    @Cerbrus He's pointing out a false assumption that you're making, not saying you should be using a different term to describe it. By assuming that the problems are trivial, when that assumption isn't a given, your argument doesn't hold.
    – Servy
    Commented Apr 20, 2016 at 18:56
  • 1
    It's not a false assumption. jQuery answers are often posted on JS questions, where the JS solution is fairly straightforward, if not trivial. If you frequent javascript, you'll see it happen all the time. Any way, the triviality of the answer is irrelevant. My point that's still unanswered is: we don't want to encourage the use of libraries for anything and everything, if the language supports the required functionality natively. I have yet to see @podiluska respond to that.
    – Cerbrus
    Commented Apr 20, 2016 at 19:11
  • 1
    jQuery isn't exactly a library for anything and everything. It's a write once so it will actually work library. It's whole purpose is to abstract the vendor specific cruft so you don't need a bunch of conditionals branching because it's one browser or another. You end up with maintainable code that Just Works. For those of us employed in places where productivity matters, jQuery is a godsend. There's nothing worse than dealing with support tickets because some broken obscure outdated browser doesn't work and you have to address it because it's important to the client and the user can't upgrade
    – Neil Davis
    Commented Apr 20, 2016 at 20:22
  • 2
    BINGO Kevin. Clients require that their code work for the client's customers. Often they are people in environments where the admins lock down the software and the users can't upgrade. You have to support that. jQuery makes that possible without spending hundreds of hours coding workarounds for buggy browsers. That's my point. In a perfect world, everyone would be on the latest version of their browser of choice. The real world is anything but perfect.
    – Neil Davis
    Commented Apr 20, 2016 at 20:24
  • 1
    Right, but we answerers shouldn't be expected to support your old as dirt browsers and poor security practices. Code for the future, not the past.
    – Kevin B
    Commented Apr 20, 2016 at 20:26
  • 3
    Unfortunately we coders, with jobs where we write code for clients, do need to support old as dirt browsers. jQuery ensures I get home on time instead of missing my daughter's birthday party. I make the assumption (however faulty) that most people with questions want a solution that will actually work on all 10 of the top 10 browsers. I actually skip questions where people specify vanilla javascript answers. I'm respectful :-)
    – Neil Davis
    Commented Apr 20, 2016 at 20:41
  • 1
    @user356540: I actually work at a place where we no longer support those older browsers that require a buttload of workarounds. Not having to load a library for simple stuff like array manipulation is pretty neat. In your last comment you're implying users that don't want libraries aren't writing code for clients. That's not very respectful.
    – Cerbrus
    Commented Apr 20, 2016 at 20:50
  • 3
    I'm not promoting busy foolishness, @podiluska. All I'm saying is: If it's not necessary, don't suggest a library! We don't want to send the message that the first thing someone should look for, is a library. For example, if all the user wants, is to fill a <div> with a string, are you really going to tell that user to load jQuery? Just for that one feature? Just use the native capabilities...
    – Cerbrus
    Commented Apr 20, 2016 at 20:54
  • 1
    Right, but what does any of this have to do with how we answer questions? Why should the fact that you have to support IE8 factor into whether we should answer a question about javascript with no mention of jquery, with a jquery answer or a javascript answer? We aren't writing your code for your company, we're solving your problem, you should then adapt it to your browser support needs.
    – Kevin B
    Commented Apr 20, 2016 at 21:03
  • 1
    @Cerbrus that's true in this one exact case, but when is a person only going to want to solve only that one problem. They're building a webpage, it's going to require a ton of extra things beyond simply filling a div. jQuery works wonders for Javascript and it's generally safe to assume you've got a priori in your project anyway. Personally I'd rather use a library than write a bunch of code myself since I can trust the library to be stable anyway. Commented Apr 21, 2016 at 20:01
  • 2
    "they're probably using jQuery already": That is a false assumption. An answer should work with whatever technologies are used in the question, or the question is tagged with. If there's no hint the OP is using jQuery, don't post an jQuery answer. Same for any other library.
    – Cerbrus
    Commented Apr 22, 2016 at 6:44
-16

The problem with vanilla javascript is compatibility. jQuery (almost always) works with all javascript interpreters (browsers).

So if someone answers your question and supplies a vanilla javascript that was tested and works in chrome, but the person asking the question is using opera, ie, or firefox (or worse, a mobile browser), there is a pretty good chance there will be a problem if you are doing more than alert("some text"), string manipulation or math. This is especially true if the answer includes the use of a library outside the ECMA standard, such as for AJAX, or needs to reference DOM elements.

Using jQuery just about guarantees the code will work on anything, or at least will gracefully degrade.

IMHO there's no good reason to use vanilla javascript anymore. It causes far more problems (10's of thousands) than it solves (absolute 0). So unless you want to chase down browser compatibility issues and spend a huge amount of time doing it, use jQuery.

Here are a few advantages:

No need to regression test every control on your page in every browser after a change
Far better documentation
It's easier to do complex stuff
Far more maintainable
No need to test 20+ browser versions, types, and iterations when debugging

I, like you, used to be a javascript purist. Then one day after fighting fires related to javascript compatibility for a few weeks straight (a few years ago), I said F it, learned jQuery, and have never looked back.

Vanilla javascript is for the birds. I spend my time writing useful code instead of trying to figure out why it won't work on Chrome 45.0.67123.112p or whatever, but works everywhere else.

As well, vanilla javascript that used to work, will break, frequently. There are tons of users out there in restricted environments that can't upgrade their browsers due to system administrator lockdown. Most government agencies, fortune 500 companies and hospitals are like this.

So you end up spending a lot of time doing workarounds for broken browsers if you do vanilla.

If you insist on bucking common sense, specify that you want the answer in vanilla javascript in your stack exchange question posts. There might be someone out there that will answer. Just make sure to post your browser and version, so the answer will work ;-)

6
  • 4
    Unless the question is asking about a specific browser, i don't think browser support should be a concern on SO. it's the developer's job to make it work in their browser stack. I shouldn't be expected to provide an answer that will work in IE8 after just copy/pasting it unless the question is specifically asking about IE8.
    – Kevin B
    Commented Apr 20, 2016 at 20:17
  • 1
    The key i think with your answer is this: "(a few years ago)" a LOT has changed in the past few years in regards to browser support. jQuery doesn't really bring much to the table anymore when using browsers that are still supported by their vendors (aka IE9, 11, latest -1 for firefox/chrome, etc)
    – Kevin B
    Commented Apr 20, 2016 at 20:19
  • 1
    @use I had to check you weren't my brother as that is his argument. Let me ask, what happens when a library is retired, or the function in a library is retired? Now the answer becomes irrelevant. Commented Apr 20, 2016 at 20:24
  • @IMTheNachoMan Fork it and continue development? jQuery is so widely used that someone would do it. If they didn't, I would. What happens when browser developers break stuff, which they do all the time? As an agency developer, where compatibility and productivity are paramount, jQuery, at least to me, is the right tool for the job.
    – Neil Davis
    Commented Apr 20, 2016 at 20:44
  • 5
    You would maintain jQuery? On your own? Good luck. 5 years ago, I'd have upvoted this answer. Not today. This answer is unrealistically pessimistic about the state of today's browsers. You even go as far as claiming that using a library is common sense, while it should be nothing more than a fallback. Libraries aren't the first thing you should look at if you want to program something.
    – Cerbrus
    Commented Apr 21, 2016 at 7:21
  • 5
    The problem in this answer are statements like: "there will be a problem if you are doing more than alert("some text"), string manipulation or math." "there's no good reason to use vanilla javascript anymore. It causes far more problems" "Here are a few advantages:" (Basically all of the provided reasons) "Vanilla javascript is for the birds." "writing useful code instead of <...>" "vanilla javascript that used to work, will break, frequently." "workarounds for broken browsers if you do vanilla." All of those are exaggerated or nonsense. They are enabling crap browsers.
    – Cerbrus
    Commented Apr 21, 2016 at 7:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .