If you are currently getting into front-end development, you might be confused with jQuery. It can be unclear what it does, why itâs used, and whether itâs something that is still worth learning. Letâs examine this questionâstarting with a bit of history.
How Jquery Was Helping Programmers in 2008
My first programming job was in 2008. Back then, I was helping build websites, and the main concern at that time was this: will it work in Internet Explorer 6?
Making websites work and look the same in that browser was easily about 50% of the front-end jobâand the least fun part.
jQuery was there to help with many issues related to the browser landscape at that time.
Browser Compatibility Issues
There were plenty of differences between browsers. If you tested your code in a modern browserâlike Firefox or Chromeâit was not guaranteed to work in Internet Explorer. There were a lot of differences in the CSS, and a bit less in JS.
The best practice at the time was to make the website work first in the modern browserâso itâs future-proofâand then add all the hacks necessary for it to work in the older browser.
jQuery addressed part of those issuesâthe methods it was providing were working the same in all the supported browsers. All the workarounds were removed from the application codebase.
There were many libraries with user interface (UI) components built on top of jQueryâmost notably jQuery UIâand using those libraries saved a lot of headaches with getting marketing interface elements looking correct and working across all the browsers.
Selectors
CSS has a very reasonable model for picking the elements from a document: selectors.
At that time, JavaScript was used only on the browser: so, JS and CSS were used by the same people, and they wanted the CSS selectors on the JS side too. At that time, browsers had nothing like this available natively.
This was another feature provided with jQuery. With it, you could find DOM elements with something as simple as
var element = $(â.some-classâ);
Obscure Browser APIs
The first version of JavaScript was developed in 10 daysâso there were many rough edges.
Its ad hoc nature continued over the years. XMLHttpRequest
was a key feature to turn JavaScript into a language that can be used to develop browser-side applicationsâit allowed for doing additional requests from the JS to the server. The way of using it isnât nice to read:
const request = new XMLHttpRequest();
request.addEventListener("load", function () {
console.log(this.responseText);
}
);
request.open("GET", "http://www.example.org/example.txt");
request.send();
With jQuery, the same goal could be achieved with much more readable:
$.ajax({
method: "GET",
url: "http://www.example.org/example.txt",
})
.done(function( msg ) {
// or however the result was read in jQuery 1.2
console.log(msg);
});
Solutions Provided by Modern Browsers
If you started your frontend journey recently, itâs unlikely that you ever saw those issues in practiceâunless you are maintaining an IE 6 interface to a legacy application that was fossilized in some corporate environment.
Thanks to evergreen browsers, we hardly hear about compatibility issues nowadays. If your everyday business application works, you can assume that all browsers work the same. And wait for bug reports in case that they donât all work the same.
API issues are addressed as wellâwe have document.querySelectorAll
and fetch
that provides a nice API to perform common operations with native functions.
Still a Pretty Popular Package
Even though jQuery is not in the spotlight anymore, itâs still a very popular package:
If you compare it with React, Vue, and Angular, only React has more downloads. You can expect to see it in many projects that are out there. Especially if you consider that plenty of jQuery usages will likely not come via NPMâmany will use the deprecated package manager Bower or CDN links.
Why Use jQuery in 2022?
The main reason I see is that you have a team that is accustomed to itâmaybe not necessarily frontend developers, but perhaps content authors, designers, or marketing specialists who learned to do things with jQuery and see no reason to change things.
You might also still do so for consistencyâs sake. For example, if in the codebase there are 200 $.ajax
calls, I would think twice before adding some fetch
call. It would be the start of a big refactoring, and Iâm not sure in what situation it would make sense to invest so much effort into it.
Should You Learn jQuery?
Learning it will not hurt you, but introducing it into a project can raise a few eyebrows. For greenfield projects, it makes sense to default to vanilla JS.
And if you join a project that uses jQuery heavily, you can most likely learn it on the job just fine by reading documentation (I wrote a guide for it) and getting code examples from the rest of the codebase.
How About You?
Are you using jQuery yourself? Have you seen it often in projects or job offers? Share your jQuery story in the comments!