0

What's difference between var x=10 and x=10 in gavascript

17th Apr 2018, 10:24 PM
Ahmed Negm
Ahmed Negm - avatar
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
17th Apr 2018, 10:34 PM
Luigi
Luigi - avatar
0
but it make no error if i did this var x = 30; var x = 35; document.write(x)
17th Apr 2018, 10:39 PM
Ahmed Negm
Ahmed Negm - avatar
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 :)
17th Apr 2018, 10:46 PM
Luigi
Luigi - avatar