0
What's difference between var x=10 and x=10 in gavascript
3 Réponses
+ 2
You must use the var the first time you declare the variable. Later on, until it’s still on the same scope, you can use it without re-declaring it.
Example:
var age = 30; // declaring and assigning a value
age = 35; // change its value
0
but it make no error if i did this
var x = 30;
var x = 35;
document.write(x)
0
No error occurs, because the Javascript interpreter accepts it and internally it treats the code like this:
var x;
x = 30;
x = 35;
document.write(x);
It’s more a “cleaner code” matter, other than shaving a couple of bytes off the page size :)