jQuery vs JavaScript

1. Comparison with Vanilla JavaScript

jQuery is a library that simplifies JavaScript tasks like DOM manipulation, event handling, AJAX, and animations. Below is a comparison of common tasks in both jQuery and vanilla JavaScript:

Example: Change text of a div

<!-- jQuery Version -->
<script>
$(document).ready(function(){
  $('#demo1').text('Text changed with jQuery');
});
</script>

<div id="demo1">Original Text</div>
Original Text
<!-- Vanilla JavaScript Version -->
<script>
document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("demo2").textContent = "Text changed with Vanilla JS";
});
</script>

<div id="demo2">Original Text</div>
Original Text

2. When to Use jQuery vs JavaScript

  • Use jQuery when you need to quickly build something with cross-browser compatibility, especially in older browsers.
  • Use JavaScript when performance is critical or you are building modern applications with frameworks like React, Vue, or Angular.
  • jQuery is easier for beginners and reduces code complexity for tasks like animations, DOM selection, and AJAX.

Example: Hide a div on button click

<!-- jQuery -->
<button id="btn-jq">Hide with jQuery</button>
<div id="box-jq">I will hide</div>

<script>
$(document).ready(function(){
  $('#btn-jq').click(function(){
    $('#box-jq').hide();
  });
});
</script>
I will hide
<!-- Vanilla JS -->
<button id="btn-js">Hide with JS</button>
<div id="box-js">I will hide</div>

<script>
document.addEventListener("DOMContentLoaded", function() {
  document.getElementById("btn-js").addEventListener("click", function() {
    document.getElementById("box-js").style.display = "none";
  });
});
</script>
I will hide

3. Migrating to Modern JS or Using jQuery Together

As browsers have become more standardized, modern JavaScript (ES6+) can now do almost everything jQuery was once needed for.

But if your project already uses jQuery or requires a quick prototype, it’s still fine to use it. You can even use both together — just avoid conflicts and redundancy.

Example: Use both jQuery and JS without conflict

<div id="both">I will change color and text</div>

<button id="js-btn">Change with JS</button>
<button id="jq-btn">Change with jQuery</button>

<script>
document.getElementById("js-btn").addEventListener("click", function() {
  var el = document.getElementById("both");
  el.style.color = "blue";
  el.textContent = "Changed by JavaScript";
});

$(document).ready(function(){
  $('#jq-btn').click(function(){
    $('#both').css('color', 'green').text('Changed by jQuery');
  });
});
</script>
I will change color and text

If you have any questions, feel free to ask. Thank you for reading!

Thankyou!