I know it's a silly question but I am a bit confused with this. For example, if I have an input with an ID: rad1
, is there any difference between below lines of code?
var $a = $('#rad1')
or
var a = $('#rad1')
A variable is a symbolic name for (or reference to) information. The variable's name represents what information the variable contains. They are called variables because the represented information can change but the operations on the variable remain the same.
JavaScript (jQuery) variable will have only two scopes. Global Variables − A global variable has global scope which means it is defined everywhere in your JavaScript code. Local Variables − A local variable will be visible only within a function where it is defined.
It has no special meaning. jQuery sets the global $ variable to an object with a number of special behaviors, so variables beginning with $ are often reserved for variables or values related to jQuery. This is not enforced at any level, though. You're free to use $ in variable names wherever and however you like.
It's just a coding convention and it allows you to quickly reference what type the variable is later in the code.
No there is no real difference.
It's just a convention that helps you remember that a
isn't the DOM element but it's a jQuery object.
var a = document.getElementById('a');
a.innerHTML //fine
var $a = $('#a');
$a.html() // fine
Ohhh, and by the way, neither a
or $a
are good variable names ... you should use meaningful variable names not abc characters.
Read the jQuery info tag on this very same site:
Variable Naming Conventions
jQuery wrapped variables are usually named starting with '$' to distinguish them from standard JavaScript objects.
var $this = $(this);
It's only for showing that it's a Jquery variable.
Declaring $a
you're showing that your variable is for JQuery objects, it's just a notation. So the most readable thing will be to declare Jquery variable with $ notation
var $obj=$("#obj");
And DOM element without $ notation
var obj = document.getElementById("obj");
There's no difference. It's just a coding convention to help identify that the variable represents a jquery wrapped object.
No difference its just coding convention , check this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With