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.
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.
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.
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.
- Elements that occurred only once in the array
- jQuery | error() Method
- How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
- Why does sizeof(x++) not increment x in C?
- Why should you not Hack Facebook!
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- Why card images use data-src (not src) for image in Bootstrap 4 ?
- Why PHP script does not create a directory with 777 permissions ?
- Why <big> tag is not in HTML5 while <small> tag exists ?
- Why is colspan not a known native attribute in Angular 2?
- Why Transition properties does not work with display properties ?
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- How to test a URL for 404 error in PHP?
- Error detection and Recovery in Compiler
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.