15

what is the difference between these statements? I know that "var $test" declares a jquery variable, but what is the difference of a jquery variable with a general javascript variable ?

MD Sayem Ahmed
  • 26,780
  • 23
  • 104
  • 174

4 Answers4

30

$test is a convention used to indicate that this variable is a jQuery object and you can call the standard functions on it while test could be a standard DOM element and you cannot call the val function on it for example.

For example:

var $test = $('#someId');
var test = document.getElementById('#someId');

You can do $test.text(); but you cannot do test.text();

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • So that means even if I don't use the "$" sign, I will still be able to call the standard functions on that object, right? – MD Sayem Ahmed May 21 '10 at 09:33
  • 3
    @Sayem Ahmed: Right, it is just a name. It has absolutely **no** meaning! It would be same if you ask whether variable names starting with `t` are somehow special. – Felix Kling May 21 '10 at 09:35
  • I never knew about this convention, but it does make a lot of sense! Thank you for this clarification, Felix. – malvim Dec 01 '10 at 19:28
24

Nothing. No difference. $ is a valid character in a JavaScript identifier but has no special meaning. In fact, in the ECMAScript 3 specification in section 7.6 it states that

The dollar sign ($) and the underscore (_) are permitted anywhere in an identifier. The dollar sign is intended for use only in mechanically generated code.

... although events have now probably rendered that recommendation obsolete. Indeed, the recent ECMAScript 5 spec omits the final sentence.

Tim Down
  • 292,637
  • 67
  • 429
  • 506
2

there is no difference between var $test and var test. but its always good to know what kind of data is in your variable: (for example if someone else just want to modify a function in your code without having to read, or have to console.log your vars to know whats in it)

here some examples:

var arrTest = [1, 2, a, b, c], //Array
objTest = {test: 1, test2: 2}, //Object
strTest = "test", //String
intTest = 4, //integer
$test = $("#test") //jqueryObject

but it would also work like this

var Test1 = [1, 2, a, b, c], //Array
test2 = {test: 1, test2: 2}, //Object
Test3 = "test", //String
Test4 = 4, //integer
$test = $("#test") //jquery Object

i think what confuses you is the $() form jquery.

Jquery is basically a function set on the variable name $:

var $ = function(e){ // the jquery magic }

but you can still use $somethingelse as variable.

meo
  • 28,823
  • 17
  • 81
  • 121
-1

Try critisism on JQuery.

The differences between native javascript and a framework like JQuery is zip - it co-exists.

If you wish to master javascript - skip JQuery.

I use GMap because of its functions - I am not able to develop GMap functionallity myself.

But JQuery and extJS just offers structure - no funtionallity that you cannot do yourself.

Mike

MikeyKennethR
  • 620
  • 4
  • 16