+ 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;
7 Respostas
+ 5
No!!! Better search on Google...
+ 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.
+ 4
I think so...
+ 4
Indeed but y is local now, try accessing it within a function while y is outside like this.
+ 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?
+ 1
it doesn't make any difference at all ... both works as expected. check out my code
+ 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?!