Pages like You might not need jQuery try to sell the idea that it’s easy to ditch jQuery, but the very first example is a good reason to use jQuery: one line of trivial jQuery code gets replaced with 10 lines of vanilla JS code!
aesthetics, which is just a nice way of saying that I think it sucks.
el.insertAdjacentElement('afterend', other) undoubtedly works, but
$(el).after(other) is actually palatable. While I have never been a huge fan
of how the
$() function looks, it’s massively better than what the DOM
Quick: how do you get an element’s sibling? Is it
nextElementSibling? What’s the difference? Which browsers support which? While
you’re busy trying to remember and checking MDN just to be sure I’ll just use
prev() in jQuery.
You will still need helper functions for various common tasks. Again, that YMNJQ page lists a number of them. Using jQuery is a standard way to include these helpers, instead of copy/pasting them from random Stack Overflow answers every time you need them.
While browser compatibility is much less of an issue than it was, it is still an issue, especially if you’re not in the “if it works for 85% of the world then it’s good enough for me”-crowd. Also see: Why Hello CSS doesn’t use CSS variables.
Should you always use jQuery? No, of course not. Adding any dependency comes with the cost of complexity and file size, but jQuery is not that large: the default build is 30K minified/compressed, a custom build without ajax and uncommon stuff 23K, and a build which uses querySelector instead of SizzleJS is 17K. Both the original 30K and optimized 17K seem pretty acceptable to me for many purposes.
You can look at Bootstrap removing jQuery for an example of how much effort it can be to use vanilla JS: they wrote their own helpers, had to drop IE support as it was too hard to add, made the API incompatible (“we broke everything”), and spent a year and a half working on it. If I look at the end result, I can’t really say it’s that much better.
There are some good reasons to not use jQuery: if you’re writing code that you want to be re-used by others, or if you’re writing just a small function. But bending over backwards to avoid jQuery? Yeh nah. Just use jQuery! “jQuery all the things” was probably not a good idea, but neither is “unjQuery all the things”.
I’m not married to jQuery, and I’ll happily use a “jQuery light” which patches over the JS API with something more palatable. There are a few projects, such as bonzo, $dom, zepto, and several others for AJAX and such, but many seem unmaintained. Besides, everyone already knows jQuery. Why replace it with something else unless there’s a compelling reason to? Saving 10-20K doesn’t seem worth it.
Does that mean that these web frameworks are always bad? No, few things are “always” bad (or good), but it does come with a set of trade-offs (as does jQuery, obviously).
Fundamentally, I think the web is a system to view documents, rather than an operating system. Even for many “web apps” (whatever you mean with that) a document approach works quite well (this is a topic worthy of its own post … at some point in the future).
I know you can use
window.jq = jQuery, but I’m not programming in a vacuum and would prefer idiomatic/standard code, and for better or worse, using
$is the standard way of doing things in jQuery. ↩