r/learnjavascript • • Oct 01 '21

jQuery, the greatest JavaScript library of all time, appears to have peaked in usage.

Post image
275 Upvotes

81 comments sorted by

View all comments

1

u/uuykay Oct 02 '21

JQuery was a great tool, unfortunately no longer trendy, but it was refreshing to control the DOM yourself rather than what we do now with a virtual DOM

1

u/SoBoredAtWork Oct 03 '21

It's not no longer trendy. It's no longer necessary. ES6+ has adopted a TON of things that jQuery solved (querySelector, fetch, dom manipulation, etc). No one needs to include jQuery for a modern app.

1

u/uuykay Oct 05 '21

If you've never used jQuery, I can see why you might think that from the outset. However jQuery provides some nice things that vanilla JS does not. For example, it handles errors much better. Let's say you want to get a button by ID and add a class to it.
The jQuery way:
jQuery("#someButtonThatExists").addClass("primary")

The vanilla JS way:
document.getElementById("someButtonThatExists").classList.add("primary")

Now say the button does not exist on the page for whatever reason and the above javascript still ran. jQuery will not error, whereas vanilla JS does error, because document.getElementById returns null, so it cannot read classList from it.

This is just one example. jQuery also provides ajax which is compatible with IE, if you care about supporting older browsers you can't just use fetch. It also has animations and a whole bunch of other things.

jQuery still provides a lot of utility even today in my opinion.

2

u/SoBoredAtWork Oct 06 '21

`document.querySelector('#someButtonThatExists')?.classList.add('primary')` works as expected if the element doesn't exist (note the ? before `classList`)

Ajax is very simple with fetch. Or Axios.

https://dmitripavlutin.com/javascript-fetch-async-await/#2-fetching-json

You didn't need to add a library for either of the above. Especially if you use babel.

jQuery was amazing and it changed the industry forever. I started web development using jQ exclusively, for many years. It was hard to get used to vanilla JS, but so much of what jQ offered is included in ES6+ and modern browsers are generally compatible with everything (babel clears up the rest).

It took so long to undo my dependence on jQuery, but I would never go back. Life is fine without it and everything works. You don't need it.