-1

Both the $() and jquery() have the same functionality(most of the things says that $() is shorthand for jquery()) as per my knowledge.

So why there is need to have the two methods for doing the same task?And why was there need to have shorthand for jquery() as its not too long(no need to press shift + $).

If they are different than in what sense they are? And which is more effective either $() or jquery()?

Using $():DEMO

$( "div" );

Using jQuery():DEMO

jQuery( "div" );
Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51

5 Answers5

1

$ is just a variable that is used to alias jQuery and being a variable, anything could be assigned to it. Some scripts use jQuery instead of $ is to prevent conflicts with other libraries such as prototype or different versions of jquery which both also use the $ variable.

Read: http://api.jquery.com/jQuery.noConflict/

Kiran
  • 20,167
  • 11
  • 67
  • 99
1

http://api.jquery.com/jQuery.noConflict/

Many JavaScript libraries use $ as a function or variable name, just as jQuery does. In jQuery's case, $ is just an alias for jQuery, so all functionality is available without using $.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

The jQuery site states:

"In the first formulation listed above, jQuery() — which can also be written as $() — searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements:"

For more info see this post

Community
  • 1
  • 1
chrisblomm
  • 314
  • 1
  • 7
1

If you're using jQuery alone then there's no difference. The two forms are synonymous.

If you have a second framework that also uses the $ variable than you might use jQuery.noConflict() to have jQuery release the $ variable. In this case $ will refer to your 'other' framework, while jQuery will always refer to the jQuery code.

0

No difference. Only readability at this level. I prefer the $ alias simply because it's less typing.

You can change the jQuery prefix to what ever you want in order to prevent conflicts between libraries. Although there are other tools for that too.

Lix
  • 47,311
  • 12
  • 103
  • 131