Skip to content

SyntaxError: Identifier 'x' has already been declared

Error message

                Identifier 'x' has already been declared
              

Quick summary

You declared a variable with the same name twice in the same scope using let or const — JavaScript does not allow this.

Why this happens

  • Declared the same variable name twice in the same block with let or const

  • Copied code that redeclares a variable already in scope

  • Merged code from different files that use the same variable name

Minimal example

✗ Broken code
const x = 1;
const x = 2;
✓ Fixed code
const x = 1;
const y = 2;

You cannot declare x twice with const in the same scope. Rename the second variable.

How to diagnose

  • Search for all declarations of the variable name in the current scope

  • Check if a variable was imported and also declared locally

  • Look for duplicate declarations in the same block or function

How to fix

  • Rename one of the variables to avoid the conflict

  • Remove the duplicate declaration if it was accidental

  • Move one declaration to a different scope (e.g., inside a block)

How to prevent

  • Use descriptive, unique variable names

  • Use a linter to detect duplicate declarations

Related Resources

Related Glossary

Related Lessons

  • Variables

    Learn about variable declaration rules.

← Back to language