What's the difference between var $x
and var x
in javascript?
var is the keyword that tells JavaScript you're declaring a variable. x is the name of that variable.
Difference between x++ and ++xx++ executes the statement and then increments the value. ++x increments the value and then executes the statement.
The former creates a local variable, while the latter creates a global variable. If you use strict mode, only var x = 1 would work. If you are using 'use strict' in your js file then later one will give error.
JavaScript variables have only two scopes. Global Variables − A global variable has global scope which means it can be defined anywhere in your JavaScript code. Local Variables − A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.
Nothing. People tend to use the $x syntax because it's easier to remember you're dealing with a jquery object rather than an element or an id.
In general I tend to use something similar to:
var $x = $(selector) //$x holds reference to a jquery object
var elX = document.getElementById(id); // elX hold ref to an element node
var xId = $(selector).attr('id'); //xId holds ref to an id attribute
The difference? One variable starts with $
.
And neither has anything to do with jQuery - it's just javascript.
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