Why does var allow duplicate declaration but why does const and let not allow duplicate declaration?
var
is allow duplicate declaration
xx=1;
xx=2;
console.log(xx+xx);//4
var xx=1;
var xx=2;
console.log(xx+xx);//4
But let
and const
does not allow duplicate deceleration
const yy=1;
const yy=2;
console.log(yy+yy);//Uncaught SyntaxError: Identifier 'yy' has already been declared",
let zz=1;
let zz=2;
console.log(zz+zz);//Uncaught SyntaxError: Identifier 'zz' has already been declared",
I saw something about that in here like,
Assuming strict mode, var
will let you re-declare the same variable in the same scope. On the other hand, let
will not.
But I want to know Why let
and const
doesn't allow re-declaration? and why var
does? and How JavaScript handle these three type of deceleration ?
var
The var
keyword was the only way to define variables until 2016*.
No matter where you write var x
, the variable x
is treated as if it were declared at the top of the enclosing scope (scope for var
is "a function").
All declarations of the variable within the same scope are effectively talking about the same variable.
Here is an example... you might think that within the function we overwrite the outer name
with fenton
, and add Fenton
to the inner variable...
var name = 'Ramesh';
function myFunc() {
name = 'fenton';
var name = 'Fenton';
alert(name);
}
myFunc();
alert(name);
In fact, it works just like this... the outer variable is not affected by the inner variable thanks to hoisting.
var name = 'Ramesh';
function myFunc() {
var name;
name = 'fenton';
name = 'Fenton';
alert(name);
}
myFunc();
alert(name);
- Actually, you could also declare them implicitly by not using the
var
keyword at all, in which case they would be added to the global scope. Subtle bugs were often tracked to this.
let and const
Both let
and const
are block-scoped, not function-scoped. This makes them work like variables in most other C-like languages. It turns out this is just less confusing than function-scoped variables.
They are also both "more disciplined". They should be declared just once within a block.
The const
keyword also disallows subsequent assignments - so you have to declare it with an assignment (i.e. you can't just write const x
, you have to write const x = 'Fenton'
) - and you can't assign another value later.
Some people think this makes the value immutable, but this is a mistake as the value can mutate, as shown below:
const x = [];
// I can mutate even though I can't re-assign
x.push('Fenton');
// x is now ['Fenton']
Why Does it Matter?
If you want to avoid some of the more confusing aspects of var
, such as multiple declarations all contributing to the same hoisted variable, and function-scope, you should use the newer const
and let
keywords.
I recommend using const
as your default keyword, and upgrade it to let
only in cases where you choose to allow re-assignment.
Unlike var
, let
is an ES2015 specification. The specification says:
Redeclaring the same variable within the same function or block scope raises a SyntaxError.
This is to improve scoping over vanilla var
.
Assuming you want to know whether this behavior is as per spec, check this 13.3.2
Within the scope of any VariableEnvironment a common BindingIdentifier
may appear in more than one VariableDeclaration but those declarations
collective define only one variable.
let
and const
are the recent editions, while var
is probably as old as Javascript itself.
In old days Javascript code-base didn't used to be too big to bother about programming mistakes and most probably focus was to ensure that instead of reporting the error of re-declaration of variable JS engine should handle it.