Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Why do we use jQuery over JavaScript?

I have worked on JavaScript before, and heard about the usage of jQuery. Why developers prefer jQuery over JavaScript? What are the benefits of using jQuery over JavaScript for the same property applied, for example to change the background color?


2 Answers
David Meador

jQuery is a JavaScript library, so it operates on top of JavaScript. It cannot exist on its own, so you can't use one over the other. You can use just JavaScript or JavaScript and jQuery. jQuery was introduced to make development with JavaScript easier. It will reduce the development time. Use it to add animation and even handling on your website.

jQuery simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development. jQuery is easier to use compared to JavaScript and its other JavaScript libraries. You need to write fewer lines of code while using jQuery, in comparison with JavaScript.

Let us see the following code snippet as an example of jQuery and JavaScript to change the background color:

JavaScript

function changeColor(color) {
   document.body.style.background = color;
}
Onload=”changeColor('blue');”

jQuery

$ ('body') .css ('background', '#0000FF');

As shown above, both the code snippets are doing the same work of changing the background color. But jQuery takes less code and in this way you can work around other examples, which shows that jQuery minimizes the code and is easier to use.

Alex Onsman

jQuery is a JavaScript library, introduced to ease the work done by JavaScript. jQuery, written in JavaScript was introduced to help developers by making the JavaScript development tasks much easier.

Using jQuery reduces the development time of a developer. The extra overhead of downloading the jQuery library do not increase the loading time. Therefore, jQuery is known as a fast and concise JavaScript library to simplify HTML document.

jQuery has a strong Open Source community. Lots of prewritten plugins are available to speed up the development journey. jQuery provides cross browser support with ease. Apply dynamic styling to the elements on the web page, select elements by id, attribute, classname, tagname, etc with jQuery.

Here's an example,

JavaScript Selector

var $ele = document.querySelector('#demo');

jQuery Selector

var $ele = $('#demo');

As you can see above, to get the first element with id="demo", jQuery needs lesser code than JavaScript. jQuery and JavaScript both runs faster on modern browsers ad machines, but JavaScript run slower on older browsers.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.