How to fix SyntaxError: Identifier 'foo' has already been declared
Variants
- SyntaxError: redeclaration of const 'foo' (Firefox)
Example
const foo = 'bar';
const foo = 'anything'; // SyntaxError: Identifier 'foo' has already been declared
Solution
This problem is easy to fix in one of two ways, depending on the intent:
1) If it's intended to be the same variable but with a new value, drop the second const keyword and change the first declaration to let (since const variables cannot be re-assigned).
let foo = 'bar';
foo = 'anything'; // no error
2) If the problem is a simple naming conflict, the solution is even easier - just change one of the variable names to something different (and don't forget to update any references!).
const foo = 'bar';
const qux = 'anything'; // no error