← Back to jQuery Course | Chapter 16: jQuery vs Modern JS & Interview Prep | Lesson 4 of 6

When to Use jQuery in Modern Web

jQuery is still useful when a project already depends on it. Keeping the existing API can reduce unnecessary rewrites.

Existing jQuery Projects

jQuery still earns its place in a codebase that already depends on it, where rewriting large amounts of existing, working code just to remove the dependency adds real risk without a clear, corresponding benefit. Migration effort is best reserved for situations where it actually solves a concrete problem.

Example: Existing jQuery Projects

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      console.log("jQuery still loaded:", typeof $ === "function");
    </script>
  </body>
</html>

Simple DOM Tasks

For small pages with a handful of interactive elements, jQuery can make common DOM operations noticeably more concise than the equivalent native code. Native APIs are also a perfectly good choice when a project has no existing jQuery dependency and doesn't need one.

Example: Simple DOM Tasks

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Toggle</button>
    <div id="box">Box</div>
    <script>
      $("#btn").on("click", function() {
      $("#box").toggle();
      });
    </script>
  </body>
</html>

Plugins and Legacy Tools

Many older applications rely on jQuery plugins for things like carousels, form validation, or date pickers, and some of those plugins have no direct native-JavaScript equivalent. In those cases, keeping jQuery around to support a required plugin is often the pragmatic choice.

Example: Plugins and Legacy Tools

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="datepicker"></div>
    <script>
      // Some legacy datepicker/carousel plugins have no direct native equivalent
      console.log("Loaded via jQuery plugin ecosystem");
    </script>
  </body>
</html>

When Native APIs Are Better

For brand-new projects, native browser APIs can avoid adding an extra dependency entirely. Modern browsers already provide querySelector, addEventListener, fetch, and a range of other APIs that cover most of what jQuery originally existed to simplify.

Example: When Native APIs Are Better

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Click</button>
    <script>
      document.getElementById("btn").addEventListener("click", function() {
      console.log("No jQuery dependency needed here");
      });
    </script>
  </body>
</html>

Choosing jQuery Wisely

The practical approach is to use jQuery where it genuinely solves a real need in a project — especially one that already depends on it — and to compare it fairly against native APIs and any frameworks already in use before reaching for it in something new.

Example: Choosing jQuery Wisely

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      const projectAlreadyUsesJQuery = true;
      if (projectAlreadyUsesJQuery) {
      console.log("Keep using jQuery for consistency");
      } else {
      console.log("Consider native APIs for a new project");
      }
    </script>
  </body>
</html>

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.