-1

Possible Duplicate:
Can someone explain the dollar sign in Javascript?

Why do certain javascript functions have a $ before the function delcaration? For example, here's the javascript for a JQuery slider. The source code can be found here http://jqueryui.com/slider/#rangemin

<script>
$(function() {
    $( "#slider-range-min" ).slider({
        range: "min",
        value: 37,
        min: 1,
        max: 700,
        slide: function( event, ui ) {
            $( "#amount" ).val( "$" + ui.value );
        }
    });
    $( "#amount" ).val( "$" + $( "#slider-range-min" ).slider( "value" ) );
});
</script>
Community
  • 1
  • 1
Yau
  • 153
  • 1
  • 7
  • For the downvoters: This is an usefull question and it catches all beginners in JavaScript/jQuery. I think it must be upvoted. – Ricardo Souza Oct 28 '12 at 18:11
  • @rcdmk: It has been asked [several times before](http://stackoverflow.com/search?q=[javascript]+dollar+sign). A simple search would have provided the answer. – I Hate Lazy Oct 28 '12 at 18:14
  • 2
    @rcdmk I agree, but there are other duplicate/similar questions that have already been answered. And any beginner to jQuery should be using/reading something that explains that in the beginning... – Ian Oct 28 '12 at 18:14
  • @Yau: `var $ = function(name) { alert("Hello, " + name); }; $("World");` Run this code, and you'll see that `$` is just another variable. – I Hate Lazy Oct 28 '12 at 18:20
  • Okay, right thanks. I wasn't clear if "$(function()..." meant a special function declaration in JS. – Yau Oct 28 '12 at 18:28
  • @ianpgall fair point. So we can close this one. – Ricardo Souza Oct 28 '12 at 18:32
  • @Yau In Javascript, `$(function () {});` is no special function declaration, but it is with jQuery - it's synonymous with `$(document).ready(function () {});` (if you haven't already figured that out). `$` by itself (as long as jQuery, or technically a special library that uses it, is included) means something, and with jQuery, it is a reference to the jQuery function as everyone has pointed out. Passing different things to it mean different things, and passing a function to it means to run on document ready. – Ian Oct 28 '12 at 18:36

4 Answers4

8

The $ isn't a JavaScript thing, it's just a shortcut used by jQuery. $ is a valid identifier name in JavaScript, and it's short, so it was popular with libraries (jQuery, Prototype, and probably a couple of others used it). $ is just an alias for the symbol jQuery.

The code you've quoted there is calling the $ function and passing in a function reference (in this case, for a function defined inline). When you pass a function reference into $ (or jQuery), you're asking the jQuery library to run that function when the DOM is ready. It's a shortcut for $(document).ready(...). Details in the documentation.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

They refer to the library that is loaded by some files added. $ may refer to jQuery or Prototype etc, but most commonly jQuery nowadays

The $ you are refering to here is jQuery

AnandVeeramani
  • 1,526
  • 9
  • 15
1

These are jQuery functions. $ just an alias for the jQuery function. You'd need the jQuery library to use that code.

sachleen
  • 30,730
  • 8
  • 78
  • 73
0

$ is a regular character in JavaScript. Therefore, it can be used as a variable:

var $ = 5;

But this character has been specialized by certain frameworks to do things like you see on that site. It's an object will multiple methods used to facilitate the headaches caused by common misfunctionality/non cross-browser quality of code. window.$ has been defined as a function, so that's why you see the call operator being applied to it:

$( .. )

In the call operator, a function expression is passed.

$( function() {} );

As defined by the $ in jQuery, whatever is in the function expression will be immediately-executed when the browser loads. A common variant of this syntax is the following:

$(document).ready(function() {

});

which means roughly the same thing.

David G
  • 94,763
  • 41
  • 167
  • 253