Why error "$ is not defined" occurred in jQuery ? Last Updated : 07 Nov, 2020 Comments Improve Suggest changes Like Article Like Report One of the most common errors faced by jQuery developers is the '$ is not defined' error. At first, it may seem like a small error, but considering the fact that more than 70 percent of the website uses jQuery in some form or other, this may turn out to create a huge mess. Reason behind this error: This error basically arises, when the developer is using a variable, before declaring it in the script. Example: JavaScript // ReferenceError: num is not defined num; declaration var num; // No more errors data; Output: In the above example, we see that 'num' has been called before it was declared. This is why ReferenceError: num is not defined was thrown in the first line. In the third line, 'num' is called again. However, no error will be thrown this time, as the variable has already been defined in the second line of the script. This is a very common error. The best way to avoid this is to hoist all the variables and functions before calling them. Have a look at another example. Example: JavaScript //reference error process(); process = function(){ var a = 2; console.log(a); } // no error process(); Output: Most common reasons for this error: Embedding jQuery plugin before the jQuery script file '$' is used to declare any variable in jQuery. A plug-in is basically a code chunk written beforehand. These chunks use predefined jQuery functions and methods. Therefore, it is necessary to embed the jQuery script file before the plugin file. Otherwise, the application won't understand the jQuery coding. Correct Order: JavaScript <script src="/lib/jquery.min.js"></script> <script src="/lib/jquery.plugin.js"></script> CDN hosted jQuery problem: It is possible that the CDN hosted jQuery version, used for the website, might have been blocked on the customer's connection. This type of issue is generally observed on IP addresses originating from countries like China, Indonesia, Korea, etc. To avoid this issue, it is better to provide a locally-hosted fallback version of jQuery. Example: JavaScript //an external CDN link <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> //fall back to local jQuery <script> window.jQuery || document.write(' <script src="http://www.mywebsite.com/jquery.min.js"><\/script>')) </script> Comment More infoAdvertise with us Next Article Why error "$ is not defined" occurred in jQuery ? sanchit496 Follow Improve Article Tags : Technical Scripter Web Technologies JQuery jQuery-Basics Similar Reads What is the use of ready() function in jQuery ? In this article, we will see how to use ready() function provided by the jQuery library. The ready() function is used to execute some javascript code only when the HTML DOM is fully loaded. We should not manipulate the DOM until it is fully loaded. ready() method comes very handy to detect when the 1 min read What is the starting point of code execution in jQuery ? The jQuery starts its code execution from the $(document).ready() function which is executed whenever the whole HTML DOM is loaded and is totally rendered by the browser, so that the event handlers work correctly without any errors. This $(document).ready() function load the script only after the wh 2 min read How to define jQuery function ? Defining the function in jQuery is different from JavaScript, the syntax is totally different. A function is a set of statements that takes input, do some specific computation and produce output. Basically, a function is a set of statements that performs some specific task or does some computation a 2 min read How to Use jQuery $(this) in Event ? The scope of this keyword changes according to the scope of the object. The $(this) keyword will refer to the selected element to which the event is attached when used in an event. In this article, we will see the implementation of $(this) with an event in different conditions. Syntax:$('element_sel 2 min read jQuery deferred.done() Method This deferred.done() method in jQuery is used to add handlers which are to be called when the deferred object is resolved.Syntax:Â deferred.done(Callbacks [, Callbacks])Parameters:Callbacks: This parameter specifies a function, or array of functions, which are called when the Deferred is resolved.Cal 1 min read jQuery deferred.fail() Method The deferred.fail() method in jQuery is used to add handlers which are to be called when the Deferred object is rejected. This method accepts one or more than one arguments, which can be either a function or an array of functions. Callbacks are executed in the same order they were added. When the De 2 min read How to Fix "ReferenceError: document is not defined" in JavaScript? The "ReferenceError: document is not defined" error in JavaScript is a common issue that occurs when trying to access the document object outside the browser environment such as in Node.js. This error can also occur if the script is executed before the HTML document is fully loaded. In this article, 2 min read How to check element exists or not in jQuery ? Checking if an element exists in jQuery involves selecting the element and verifying its length. If the selected element's length is greater than 0, the element exists in the DOM; otherwise, it doesn't. This is useful for conditionally manipulating elements. There are two ways to check whether an el 3 min read What does dollar sign ($) means in jQuery ? The $ sign is nothing but an identifier of jQuery() function. Instead of writing jQuery we simply write $ which is the same as jQuery() function. A $ with a selector specifies that it is a jQuery selector. It is given a shorter identifier as $ just to reduce the time for writing larger syntax. It co 2 min read Like