+ 1

Using var keyword

What is the difference between using a variable declared with the var keyword and not declaring it with var? example: var x = 5; x = 5;

21st Dec 2016, 5:11 PM
qobus
7 Answers
+ 5
No!!! Better search on Google...
21st Dec 2016, 5:39 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
If you don't initialize it with var, JS will assume it's an already existing variable and upon value assigning it'll throw an error if it's not instantiated/initialized.
21st Dec 2016, 5:15 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
I think so...
21st Dec 2016, 5:21 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 4
Indeed but y is local now, try accessing it within a function while y is outside like this.
21st Dec 2016, 5:29 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 1
well i thought of that, but i've never had any problem when i removed "var". does using a variable without var only work in a function body?
21st Dec 2016, 5:19 PM
qobus
+ 1
it doesn't make any difference at all ... both works as expected. check out my code
21st Dec 2016, 5:27 PM
qobus
+ 1
there is no problem accessing y in a function. i mean var makes your code cleaner because you see when it gets declared but is that really the only difference?!
21st Dec 2016, 5:38 PM
qobus