views:

95

answers:

2

Hi,

I know a bit of javascript, and can work fine with jquery. i just don't get why everything is referenced from $(). My understanding is that $ is never needed in javascript (unlike for example php, where every variable is prefixed with $).

ive looked through the source code and it doesnt really make sense. Is is just $ is the function name (eg, it could have easily have been jquery(), but they selected $?) i assume not though, as i don't think $ is valid in function names in JS??

+12  A: 

$ is just a global variable that's also a reference to the jQuery function, it's $ on purpose so it's less to type. $ is perfectly valid for a function name in ECMAScript:

function $(){}; alert(typeof $);

Note that if you're using multiple libraries you can use function scope to avoid clashing dollar sign variables, eg:

jQuery.noConflict();
(function($){ 
    $('body').hide();
})(jQuery);
meder
+4  A: 

It's because $() is short for jQuery() and it's a function that wraps all of jQuery's methods into a nice simple function. It's really amazingly well design, John Resig has built an excellent framework.

Also, $ is a valid function name.

Joseph Silvashy