Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript variable declaration

Tags:

javascript

What's the difference between following types of var declaration in JavaScript?

$x

var x

var $x
like image 957
Amarjeet Hans Avatar asked Dec 06 '22 22:12

Amarjeet Hans


2 Answers

$x and x are simply two different variable names. Like var x and var y. Simply using $x is an implied declaration, except that if $x exists in a higher scope, you'll use it. declaring var $x sets up a new $x in the current scope which avoids conflicts with any other $x at a higher scope.

like image 193
Surreal Dreams Avatar answered Dec 21 '22 09:12

Surreal Dreams


No difference except for scope. $x is just a variable name like x. var creates variables in a local scope, otherwise they are global.

This has nothing to do with jQuery, really.

like image 30
Jamie Treworgy Avatar answered Dec 21 '22 11:12

Jamie Treworgy