SyntaxError: redeclaration of formal parameter "x"

訊息

SyntaxError: redeclaration of formal parameter "x" (Firefox)
SyntaxError: Identifier "x" has already been declared (Chrome)

錯誤類型

哪裡錯了?

當相同的變數名作為函式的參數、接著又在函式體(function body)內用了 let 重複宣告並指派時出現。在 JavaScript 裡面,不允許在相同的函式、或是作用域區塊(block scope)內重複宣告相同的 let 變數。

實例

在這裡,「arg」變數的參數被重複宣告。

js
function f(arg) {
  let arg = "foo";
}

// SyntaxError: redeclaration of formal parameter "arg"

If you want to change the value of "arg" in the function body, you can do so, but you do not need to declare the same variable again. In other words: you can omit the let keyword. If you want to create a new variable, you need to rename it as conflicts with the function parameter already.

js
function f(arg) {
  arg = "foo";
}

function f(arg) {
  let bar = "foo";
}

相容性註解

參見