Javascript, why not remove the var keyword?

almost all javascript books say that

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

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

global foo; 

we use local variables almost all the time, right? is there any good reason? Thanks for any help.


edit: Thanks for your help, I thought that there must be a good reason showing that using var is better, so I am not trying to change the language, what it is.

+4
source share
5 answers
 function A() { var foo; // variable in the scope of function A function B() { foo; // variable still in the scope of function A } } 

If the choice were “Narrowest Scale” or “Global Area”, this would not be possible.

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

Another point - there is no easy way to remove something from JavaScript. Each function (even hacks) is already used on thousands of sites, and the removal functions will destroy these sites. JavaScript can be extended. Or create a new JS that will be incompatible with previous versions.

+6
source

This is exactly how the language was developed. If a variable is automatically allocated, it is allocated in the global area.

That makes a lot of sense if you think about it. In what area should the variable be highlighted? The compiler is not able to find out the purpose of programmers with explicit declaration

For better or worse, JS was designed the way it was. I believe that allowing variables for automatic declarations was a mistake, but given that it exists in JS, it makes sense to have them global, since JS does not have a block level area.

+3
source

Using the var keyword inside a function, declares a variable in the local scope, which prevents overwriting any global variable. The idea is to play it safe and use var. If you know what you are doing (100% sure that you will not overwrite any global variable), do not hesitate to drop var.

+1
source

Well, I will try to explain how this works again. There is an ECMAScript Global object that is the “root” of everything else. In window browsers, the object implements Global . So:

 function assert( condition, message ) { if ( !condition ) if ( typeof message != 'undefined' ) alert( message ); else alert( 'Free Consulting just talks rubbish' ); } // assuming global scope assert( this == window, 'noes!' ); // and Global context var spam = 'for all you python lovers'; // becomes a property of Global assert( spam == window.spam, 'there not much spam in it' ); // same function eggs () { // becomes a method of Global actually assert( spam == window.spam, 'shut up!' ); // unqualified spam available here through closure assert( arguments.callee == window.eggs ); // and again } eggs(); 

Mrs Conclusion: JavaScript is a great language with its own specific features, so do not apply other language skills to JS (this makes Douglas Crockford a sad panda :)

+1
source

Source: https://habr.com/ru/post/1344443/


All Articles