uncaught typeerror: $ is not a function

0 min read 156 words

The dollar-sign ($) in Javascript has for many years now been associated with jQuery.

When you see this error:

“uncaught typeerror: $ is not a function

It almost always means that a jQuery piece of code is being run before the jQuery library has been initialised and loaded.

Oftentimes, your code will wait for jQuery to load before continuing any processing, this is almost always done as follows:

$(document).ready(function(){
    // jQuery code is in here
});

However, it’s not uncommon to get an “TypeError: $ is not a function ” error in response.

Some developers resolve this by wrapping their code as follows:

(function($){
    // jQuery code is in here
})(jQuery);

What this does is, it forces a scoped jQuery variable to be re-aliased within an anonymous function in your application.

Alternatively, you can just do the following:

jQuery(document).ready(function($){
  //you can now use $ as your jQuery object.
  var body = $( 'body' );
});
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags