Reputation: 2594
Chrome 67 (the latest version as of this writing) lets me do this
function foo(x, y) {
var x = 5;
console.log(x, y);
}
foo(1,2);
// prints 5 2
but not this
function foo(x, y) {
let x = 5;
console.log(x, y);
}
foo(1,2);
// Throws a SyntaxError: Identifier 'x' has already been declared
Is this behavior standard-defined?
Upvotes: 0
Views: 134
Reputation: 7819
Using the old var
keyword you were free to declare a variable multiple times without issues (well... this was an issue by itself).
The const
and let
keywords have a better behavior. In your case: you don't need to re-declare x
as it's already declared as an argument of the foo
function, so you can assign something to it directly (overriding parameters is not something very clean, but is legit).
Upvotes: 2