Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript, why not remove var keyword?

almost all javascript books said that

always use var keyword when you declare variables, because without var, the variable will be declared as global variable.

then, why not remove var keyword, make default declaration as local scope? like Python, if you want to use global variables, you write:

global foo;

we use local variables almost all the time, don't we? is there a good reason? Thanks for any help.


edit: Thanks for all your help, I thought there must be a good reason shows that using var is better, so I was not attempting to change the language what it was like.

like image 462
netroyal Avatar asked Dec 07 '22 22:12

netroyal


1 Answers

var globalInt = 5;

function Hello()
{
    // Hey, give me back my var keyword!
    // I want to assign a global existing variable
    globalInt = 7; 
}

Another point is there is no easy way to remove something from JavaScript. Every feature (even hacks) is already used on thousands of sites and removing features will break those sites. JavaScript can only be extended. Or new JS should be created which will be incompatible with previous versions.

like image 103
Snowbear Avatar answered Dec 24 '22 15:12

Snowbear