2

I'm confused by the different uses of the $ in jQuery. Take for example:

$(document).ready(function($) {
  $("#nav_lat").slicknav({prependTo:"#mobile_menu"});
});

The first $ could be replaced with "jQuery" and I realize that the second $ is being passed into the anonymous function but what does it mean? And then the last $, what does that mean?

DCR
  • 14,737
  • 12
  • 52
  • 115
  • Your answer is here : http://www.authenticsociety.com/blog/JavaScript_DollarSign – mojorisinify Feb 23 '16 at 16:05
  • It is just a reference to the jQuery object library itself so that you can access the properties and methods of the library. Just like Underscores uses _ to reference the library. – Korgrue Feb 23 '16 at 16:05
  • I'd also like to add that you can create your own jQuery object that doesn't conflict with other libraries with: `var j = jQuery.noConflict();` – Derek Pollard Feb 23 '16 at 16:07

2 Answers2

3

The $ is just a function. It is actually an alias for the function called jQuery, so your code can be written like this with the exact same results:

jQuery(document).ready(function() {
  jQuery("#nav_lat").slicknav({prependTo:"#mobile_menu"});
});

The reason why you pass $ - as in function($) - is to avoid conflict in case where there are other libraries who use $ as the placeholder.

Sooraj
  • 9,717
  • 9
  • 64
  • 99
1

$ is short for JQuery. Sometimes when you are using multiple libraries you want to use JQuery instead because $ could be used by both libs.

But using only $ is faster when you know it will use the JQuery lib.

EDIT: Fun fact, you can write the ready function like this:

$(function(){
});
Chris
  • 987
  • 6
  • 24