r/webdev • • Feb 13 '19

Bootstrap 5 will remove jQuery as a dependency

https://github.com/twbs/bootstrap/pull/23586
1.5k Upvotes

398 comments sorted by

View all comments

72

u/Grrrucha Feb 13 '19

noob here, can someone explain me like I'm five, what is so bad about jQuery? I use it constantly (mostly for creating tags for tag manager) and whenever I learn that there is no jQ on a page that I have to work on I get annoyed cause it means much more writing to get something a simple jQuery method would accomplish.

212

u/kilakewe Feb 13 '19 edited Feb 13 '19

Think of jQuery as a millennium Falcon Lego box kit. Out of the box, it makes something pretty cool, but if your trying to make a little Lego dog, you've got a whole lot of pieces that never get used, but you have to carry them around anyway.

This is how vanilla JavaScript fans feel about jquery

Edit: typos

59

u/bmey Feb 13 '19

To piggy back on this, here is a great resource for how to do the same things jQuery does with vanilla JS: http://youmightnotneedjquery.com

46

u/shellwe Feb 13 '19

You don’t need it... but I see that first example on JSON and the jquery is 1 line of code where the vanilla JS is 10 lines. It’s hard to be motivated to do the 10 line version when I can do the 1. It would be nice to see ES2015+ examples since we would use Babel to scale back to 9 anyway.

53

u/lookingforusername Feb 13 '19

TBF the "equivalent" examples are a bit dated... For example, a modern version of

$.getJSON('/my/url', function(data) { });

would be

fetch('/my/url').then(res => res.json()).then(data => { });

or async style:

res = await fetch('/my/url');

data = await res.json();

// do stuff with data

19

u/SupaSlide laravel + vue Feb 13 '19

Of course the equivalent examples are dated, they're intended to work in old versions of IE so that they support the same versions as jQuery. You can't use fetch instead of jQuery if you need to support IE9.

9

u/WhyLisaWhy Feb 13 '19

Just use something like babel to compile your ES6 code. Jquery really isn't necessary anymore and honestly its better to just get more fluent with ES6 for your career anyways. None of the technical architects at my company are interested in new hires knowing Jquery anymore.

5

u/dons90 Feb 14 '19

None of the technical architects at my company are interested in new hires knowing Jquery anymore.

Pretty much this. If you ever plan to work in modern front-end development, you should ditch JQuery, preferably yesterday.

→ More replies (1)
→ More replies (1)
→ More replies (3)

16

u/gonzofish Feb 13 '19

Yes, the developer ergonomics is better but you end up shipping ALL of jquery to do it

→ More replies (5)
→ More replies (2)
→ More replies (2)

22

u/nyxin The 🍰 is a lie. Feb 13 '19

And out of your Millennium Falcon LEGO kit, we all know you only REALLY use the selector engine and click controls for the most part when your building your ship.

So we decided we’d rather build a faster millennium falcon from scratch without all the extra pieces we don’t need.

(ps. I don’t hate jquery. It had its time and place and made a lot of things better. We’ve moved on and these tools are no longer needed for their intended purpose. jQuery needs to be remembered fondly and forgotten.)

6

u/[deleted] Feb 13 '19

A faster Millennium Falcon, you say? How many parsecs did it take in the Kessel Run?

→ More replies (2)
→ More replies (30)

49

u/cheekysauce Feb 13 '19

Development in general is moving towards more declarative than imperative frameworks and libraries.

With something like React you're describing what you want rendered, not how to render it. A component is only responsible for the bits it touches, and doesn't affect other parts of the page, and nothing else is going to come along and pull the rug out from under you.

With jQuery you're grabbing random bits of the page and mutating them without a cohesive rhyme or reason. You have to look over the entire code base to understand how the application works. Some other bit of code somewhere could change something out from underneath you and you'll never know about it till it breaks. This is how you end up with spaghetti code.

jQuery was useful in it's day as a nice abstraction over an ugly DOM API, and can be suitable for tiny minor jobs in a single page, but is not fit for purpose for architecting a full application.

16

u/Brachamul Feb 13 '19

Yeah this is a major radin.

For anything simple, vanilla JavaScript works much better than it used to.

For anything mildly complex, VueJS or ReactJS make the app more readable and fixable.

→ More replies (1)

28

u/wherespoochie Feb 13 '19

I don't think jQuery is necessarily bad, it's just becoming harder to justify its existence.

Writing any JavaScript was a nightmare back when we had IE6 and IE7 in regular use alongside early versions of Chrome and Firefox since IE would just ignore so many standards; your site would be a work of art in Firefox but would look like a hot mess in IE. jQuery (and prototype, mootools etc) made it so you could write code once and be fairly sure it would work across all browsers. It also added some really useful ways of accessing DOM elements, looping over arrays, manipulating CSS on the fly

These days IE is almost dead, most browsers tend to behave themselves and ES6 is adding a lot of the functionality I used to use jQuery for. In fact, the only reason I need to include it these days I because I use bootstrap on most projects.

24

u/DonPhelippe Feb 13 '19

IE is almost dead

Laughs in monolithic organisation

9

u/SpliceVW Feb 13 '19

Microsoft the other day said it wasn't a browser anymore. It's now a compatibility tool.

11

u/DonPhelippe Feb 13 '19

Keeps laughing in monolithic organisation since XP still work and noone will ever ask for new OS/application development even if they have to install Citrix that will share IE, since it's actually cheaper

Source: have seen some crazy stuff in both public and private sector

10

u/giupplo_the_lizard Feb 13 '19

JQuery is not at all bad. It's just less and less a necessity

17

u/noknockers Feb 13 '19

Most of the stuff you can do without jQuery these days.

0

u/[deleted] Feb 13 '19

[deleted]

7

u/DreamingDitto Feb 13 '19

I think he meant without extraordinary effort.

→ More replies (2)

16

u/tremby Feb 13 '19

I'm curious. Give an example of a "simple jQuery method" and what you would have to do instead without jQuery.

You might find this instructive: http://youmightnotneedjquery.com/

10

u/Grrrucha Feb 13 '19

I was thinking about adding event listeners (.on) or easily iterating over some content (.each) , but I checked your link and compared it and you're right. Thanks for the site it will come in handy!

9

u/tremby Feb 13 '19

It's a bit out of date in that if you are able to use ES6 features like for..of loops, things get quite a bit nicer than the examples on that site. And I haven't been asked to support IE10 in a few years now, let alone 9 and 8.

You mentioned "on". One thing to be aware of which is not clear in the addEventListener example is that while jQuery will let you set listeners for all elements in its list at once, you'd need to loop over the elements (this is one example of where for..of is really nice) and run an addEventListener on each. Similarly event delegation, which "on" makes easy, is a little more fiddly without jQuery. It's not difficult, just not quite so terse.

Off the top of my head (and I'm on mobile) to set listeners on a bunch of elements (this covers both "on" and "each"):

for (const a of document.querySelectorAll("a.foo")) {
  a.addEventListener("click", handlerFunction);
}

To do it with event delegation:

document.getElementById("my-container").addEventListener("click", (event) => {
  const a = event.target.closest("a.foo");
  if (a == null) return;
  // Do something with a, the clicked anchor element
});

Note that depending on target browsers you might need polyfills, such as for Element.closest.

→ More replies (9)
→ More replies (1)
→ More replies (3)

20

u/[deleted] Feb 13 '19

I find jQuery as tool for lazy people(I'm using jQuery everywhere where I can myself).

It's easier to do things in jQuery, but there are loading time cost for it. But we could just do same tasks in plain javascript with paying more effort to it.

https://i.stack.imgur.com/sGhaO.gif

→ More replies (4)
→ More replies (23)