
Many, when it comes to jQuery, say this: “Just use regular JavaScript. You don't need the jQuery library. ” What can I say? I do not need many things, but despite this, it is good when they are. So jQuery. I don’t need this library, but it’s definitely a pleasure to have it at hand.
Sites like
You might not need jQuery (YMNJQ) promote the idea that it is very easy to get rid of jQuery. But the very first example on this site demonstrates a good reason for using jQuery. There, a line of simple code on jQuery is replaced by 10 lines of the usual JS!
Most of the JavaScript APIs, especially those aimed at working with the DOM, offend my aesthetic feelings. This is - if you gently express my attitude towards them. Speaking bluntly, I think these APIs are a total nightmare. For example, the construction
el.insertAdjacentElement('afterend', other)
certainly works. But where
$(el).after(other)
looks nicer. Although I never really liked the appearance of the
$()
function, it is incomparably better than what the standard DOM APIs give us. I know that instead of
$()
you can use something like
jQuery(sel)
or
window.jq = jQuery
. But I'm not programming in a vacuum. Therefore, I prefer to use standard techniques. I don’t know if this is good or bad, but the
$()
construction has become the standard when using jQuery.
Try to quickly think about how to get a neighbor element of another element using DOM. What to use for this -
nextSibling
or
nextElementSibling
? What's the difference? And which browsers support this or that method? While you are trying to remember it and look at the MDN, checking yourself, I, using jQuery, just write
next()
or
prev()
.
')
The implementation of many common operations implemented in the JavaScript-API is inconvenient. I could cite a whole list of such operations here, but for me this is perfectly done on the YMNJQ page.
To solve various simple problems using JS tools, auxiliary functions are needed. And on the site YMNJQ, again, you can find a lot of examples. Using jQuery is a standard way to include such auxiliary functions in your code. At the same time, the programmer does not need every time he needs something like that, snatching pieces of code from the first answers that came up on Stack Overflow.
Although these days browser compatibility issues have lost their urgency, they are still relevant. Especially - for those who do not belong to the camp of developers, who believe that if something works in 85% of browsers, then it suits them. By the way,
here is the material on why Hello CSS doesn't use CSS variables.
Should I always use jQuery? No, of course not. Any additional dependency is an increase in the complexity of the project and an increase in the amount of its code. But jQuery is not a big library. The standard build, minified and compressed, takes 30 Kb. A custom build without ajax and without rarely used features is 23 Kb. And the build, in which
SizzleJS
used instead of
querySelector
, takes only 17 Kb. I, for solving a variety of tasks, are quite satisfied with the standard build of 30 KB and optimized, with a size of 17 KB.
Here you can look at how much effort is put into removing jQuery from Bootstrap and switching to regular JS.
The developers wrote their own helper functions. They had to abandon support for IE, since such support turned out to be very difficult to implement. They made an incompatible API (“we broke everything”) and spent a year and a half on it. I can not say that what happened is much better than what happened.
I understand the reasons for translating Bootstrap to regular JS. For example, developers want to use Bootstrap with Vue.js, or something like that. And Vue.js and jQuery in one project is a little overkill. I am a big supporter of reducing the amount of unnecessary code on the web (
here and
now - a couple of materials about it). But here I propose to look at the situation from a pragmatic and realistic point of view. Is the inclusion of jQuery code in Bootstrap 17 KB - is it so bad? When I say that in order to browse sites like Medium or the New York Times, you need to download more megabytes of JavaScript code, I’m being asked if I’m on a 56-kilobit modem line to protect the situation. Megabyte JS - this is normal. Is it really 17 Kb jQuery is a heavy burden?
There are good reasons for jQuery not to use. For example, jQuery is not needed if you are writing code that you want to share with others, or if you are creating a small function. But why turn it inside out just to not use jQuery? Why all this effort, if you can just write
$()
? I don’t think jQuery should be used everywhere and always, but I don’t think it’s right to be a fanatical rejection of jQuery.
I want to note that I am not married to jQuery. I would love to use something like “jQuery light” - a kind of library that covers the flaws of the standard API, giving the programmer something more enjoyable. The YMNJQ website recommends, among others, the
bonzo and
$ dom libraries, designed to solve various problems. But many of them, it seems, have not been supported for a long time. In addition, many already know jQuery. Why change jQuery for something else without good reason?
Many readers may wonder what I, in line with this material, can say about Vue.js, React and other modern frameworks. But the purpose of this article is to compare the usual JavaScript and jQuery, and not to offer the community "Grandiose general theory of front-end development."
Given the above, I believe that I can find quite a few reasons to use regular JS where jQuery can be used. This is mainly due to the fact that I want to create quick pages and use the simplest working constructions. At the same time, I strive to ensure that my pages can view the largest possible number of web users. Experience tells me that the shortest way to achieve this goal are the templates generated on the server, which are slightly, in the style of "progressive improvement", spiced with JavaScript. If we compare such projects with something that uses something more complex, it turns out that they are often easier to develop. Such projects are usually faster, they usually have fewer errors, and during the work on them the laptop fan does not make a noise that can wake the whole house.
Does this mean that modern web frameworks and powerful libraries are always bad? No, does not mean. Very little is worthy of “always” being called good or bad. But using the framework means making some compromises (this, of course, is also true for jQuery).
In general, I see the web as a medium for viewing documents, and not something like an operating system. And the “document approach” is good not only for ordinary sites, but also for many “web applications” (whatever they call it).
Dear readers! Do you use jQuery?
