I just started learning JavaScript. Found a statement function($) { ...}
while checking out examples. Can anyone tell me what function($)
means?

- 349,597
- 67
- 533
- 578

- 485
- 1
- 4
- 5
-
1See javascript clouser's. Keep in mind that **java !=javascript**. – Suresh Atta Oct 02 '13 at 13:14
-
http://stackoverflow.com/questions/7341265/what-does-function-mean-in-javascript – Hanky Panky Oct 02 '13 at 13:14
-
1`function($)` means that you are defining a function whose first argument will be bound to the identifier `$`, which is just another identifier, no special semantics. – Marko Topolnik Oct 02 '13 at 13:15
-
You can see a nice example here on stackoverflow. http://stackoverflow.com/questions/2937227/what-does-function-jquery-mean This will give a good understanding of what it does. Check this out – Oct 02 '13 at 13:19
2 Answers
It means "This defines a function. When it is called: create a local variable called $
and assign the value of the first argument to it."

- 914,110
- 126
- 1,211
- 1,335
First off: This creates a function where $
is the first argument passed to it. It could look like this potentially:
function dollar($){
alert($);
}
dollar("hello")
//alerts "hello"
Typically this is used when you want the $
to mean jQuery
.
For example:
(function($){
//stuff where $ === jQuery
})(jQuery)
Means that jQuery will be passed into the $
variable for the scope of anything that happens in that function.
This can be useful if you have multiple libraries within the global scope that may use the $
variable, but you have a modular plugin that refers to the necessary library as that and you don't want to rewrite the whole thing.
Note: It does not always mean jQuery
but in about 80% of cases it will. Otherwise it is just a convenient way to bind a library to a shorter variable within a certain scope.

- 2,754
- 1
- 21
- 34