JavaScript is an interpreted, object-oriented programming language. It's mainly used to make webpages interactive but can also be used to code server-side applications.
On the other hand, JQuery is a library written in JavaScript that provides a large number of features.
Since JQuery is just more JavaScript, everything that JQuery can achieve can be achieved using JavaScript. However, the same features would take more effort and lines of code in JavaScript than in JQuery.
The table below summarizes the significant differences:
JavaScript | JQuery |
It is a language. | It is a library. |
It tends to require more lines of code for implementing a function. | It uses fewer lines of code as many functions are provided and do not need to be implemented. |
It is processed directly by the browser and, as a result, can have better performance. | It is comparatively slower as JQuery code must be converted to JavaScript first. |
It may require extra code to ensure correct execution across different browsers. | It is compatible across many browsers and will behave the same over different browsers. |
JavaScript is written inside the <script> tag. | JQuery is also written inside the <script> tag, but the library file must be downloaded or imported from from a JavaScript file hosting server. |
The main differences in code can be observed in the following:
JQuery simplifies the code required for implementing the above features.
DOM objects are visual or functional objects that are part of the
Note: Click here to learn about the DOM.
JavaScript accesses DOM objects using the following syntax:
document.getElementById('id')
The id
string parameter is the HTML ID of the element we want to manipulate.
On the other hand, JQuery implements JQuery objects which are wrapper objects that consist of one or more DOM objects.
Below is the syntax needed to access JQuery objects:
$('#id')
The $
operator is used to access objects, and the HTML ID of the desired element is passed as a string.
Note: The string parameter must start with
#
if an ID is being passed.
Events are anything that the browser or user does. This includes clicking, loading, typing, and so on.
The example below shows one way to implement a button click event in JavaScript:
addEventListener()
method. The first parameter indicates the type of event, and the second parameter is the function to be executed after the event.Event handling in JQuery is much simpler.
click()
method specifies the addition of a click event.h1
heading element and change its display text.Animations are achieved by making changes in the styling of an element.
JavaScript does not have an explicit animate function. Instead, we must implement all animations by changing the CSS of an element.
We can implement a simple animation of a sliding box in JavaScript in the following manner:
box
element by ID and attach a click event.frame()
function every one millisecond.frame()
function. If the x coordinate of the box (x_pos
) is less than 500
, we increment the x coordinate. Otherwise, we reset the interval.On the other hand, JQuery has a powerful animate()
method that significantly reduces the code needed for animations.
The following example shows the same animation using JQuery:
box
, and specify a click
event is specified.animate()
method and specify the required animation. In this case, we want the box's left side to move to position 500
.Ajax techniques allow web applications to send and receive data from a server asynchronously.
In JavaScript, the browser-provided XMLHttpRequest
object is used to make HTTP requests.
Given below is an example of an HTTP request using JavaScript. In this example, we request a user's data from a public API and display the retrieved first name.
In the HTML section of the code, we create a p
element and a button
. In the JavaScript section of the code, we add an event listener on the button
. The explanation for the JavaScript code is as follows:
<p>
is initially hidden.button
element is retrieved, and a click event is added.XMLHttpRequest
. This will help us send the HTTP request.The implementation for a simple GET request looks very daunting using just JavaScript. Fortunately, we can implement the same function in JQuery much more easily.
The same example is reworked using JQuery. The explanation for the JavaScript code is as follows:
<p>
is initially hidden.Free Resources