JavaScript
Web Development
Coding Errors
Debugging
jQuery

Uncaught ReferenceError $ is not defined?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Uncaught ReferenceError: $ is not defined is a common error message that JavaScript developers encounter, especially when starting to work with libraries like jQuery. This error indicates that JavaScript cannot recognize $, which is generally used as a shortcut or alias for jQuery in scripts. This scenario can be perplexing for new developers as the cause may not always be immediately obvious.

Understanding the Error

The $ is not part of standard JavaScript but is conventionally used by jQuery, a popular JavaScript library that simplifies HTML document traversing, event handling, animating, and Ajax interactions. The error message typically appears in the console when the browser tries to execute jQuery-specific code before jQuery itself has been loaded and initialized or when jQuery is not loaded at all.

Common Causes

  1. jQuery Not Included: Before using ,jQuerymustbeincludedinyourHTMLorJavaScriptfile.IfjQueryisnotadded,orthescripttagthatincludesjQueryislocatedbelowyourscript,, jQuery must be included in your HTML or JavaScript file. If jQuery is not added, or the script tag that includes jQuery is located below your script, will not be recognized.
  2. Incorrect Path to jQuery: Even if jQuery is included, the path specified in the src attribute of the script tag might be incorrect, leading to a failure in loading the library.
  3. Conflict with Other Libraries: If other libraries also use asashortcut,itcanleadtoconflictswherejQuerydoesntgettodefineorlosesitsaliasas a shortcut, it can lead to conflicts where jQuery doesn't get to define or loses its alias.
  4. Asynchronous Loading Issues: If jQuery is being loaded asynchronously, there might be cases when your scripts run before jQuery fully loads.

Examples

Correct Loading of jQuery

html
1<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
2<script>
3    $(document).ready(function(){
4        $('body').css('background-color', 'blue');
5    });
6</script>

Incorrect Loading Order

html
1<script>
2    // This will cause 'Uncaught ReferenceError: $ is not defined'
3    $(document).ready(function(){
4        $('body').css('background-color', 'red');
5    });
6</script>
7<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

How to Fix the Error

  • Ensure jQuery is Included: Check if the jQuery library is successfully included in the page before any script that uses $.
  • Correct the Load Order: Place all jQuery dependent scripts after the jQuery inclusion line.
  • Resolve Path Issues: Verify that the path specified in jQuery's script tag is correct.
  • Handle Asynchronous Loading: Ensure scripts that depend on jQuery are either deferred until jQuery loads or are included in a jQuery ready function.
  • Avoid or Manage Conflicts: Use jQuery’s noConflict() method to avoid conflicts with other libraries or frameworks that also use $.

Summary Table

IssueSolutionPrevention
jQuery Not IncludedInclude jQuery before using $Check the script’s presence in HTML.
Incorrect PathCorrect the file path in the script tagVerify path validity with a test load.
Load Order IssuesPlace jQuery script above dependent scriptsRevise HTML structure for script order.
Asynchronous LoadingApply document ready or use async/defer attributesUse proper event handlers to manage timing.
Conflict with Other LibrariesImplement jQuery noConflict()Use distinct alias or encapsulate usage.

Additional Considerations

  • Debugging: Use browser developer tools to check if jQuery is loaded. The Network tab can be particularly useful for this purpose.
  • Using CDN: Employing a CDN link for jQuery can reduce errors related to incorrect paths and benefit from cached versions for faster load times.
  • Modern Alternatives: Consider if modern vanilla JavaScript (ES6 and beyond) can replace jQuery in your project, as it might no longer be necessary for many common tasks.

By understanding these concepts and applying the right fixes, developers can effectively manage and prevent the "Uncaught ReferenceError: $" error in their projects.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.