I just started learning JavaScript. Found a statement function($) { ...}
while checking out examples. Can anyone tell me what function($)
means?
![](../../users/profiles/68587.webp)
- 307,513
- 65
- 473
- 519
![](../../users/profiles/2762530.webp)
- 445
- 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."
![](../../users/profiles/19068.webp)
- 800,325
- 104
- 1,079
- 1,205
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.
![](../../users/profiles/1142543.webp)
- 2,688
- 1
- 18
- 33