under normal circumstances, variables declared by let or const cannot be declared repeatedly, and an error will be reported even when using var.
eg:
let a = 123;
var a = 456;
error prompt: Uncaught SyntaxError: Identifier "a"has already been declared
const C = 123;
var C = 456;
error prompt: Uncaught SyntaxError: Identifier "a"has already been declared
parameter variables are declared by default, so you cannot declare them again with let or const,
< H2 > but why can I use var declaration? What is the default declaration here? < / H2 > eg:
use var to declare parameters within the function
function foo(x = 5, y = function() { x = 2; }) {
var x = 3;
y();
console.log(x);
}
foo() //
use let to declare parameters within the function
function foo (x = 5, y = function () {x = 2;}) {
let x = 3;
y();
console.log(x);
}
foo() // Uncaught SyntaxError: Identifier "x" has already been declared