apacheatyou
apacheatyou

Reputation: 59

Why/how is everything $() based in jQuery?

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 $).

I've looked through the source code, and it doesn't really make sense. Is it just that $ is the function name (for example, 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 JavaScript?

Upvotes: 5

Views: 206

Answers (2)

JP Silvashy
JP Silvashy

Reputation: 48525

$() is the same as jQuery(). Also, $ is a valid function name.

Upvotes: 6

meder omuraliev
meder omuraliev

Reputation: 186562

$ 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);

Upvotes: 18

Related Questions