TryBugHunt

SyntaxError: Identifier 'x' has already been declared

SyntaxError: Identifier 'x' has already been declared in JavaScript means you declared the same name twice with let, const or class in the same scope. JavaScript allows one declaration per name per block.

JavaScriptPattern: Scope and closure bugs

How do I fix SyntaxError: Identifier 'x' has already been declared?

SyntaxError: Identifier 'x' has already been declared means the same name was declared twice with let, const or class in one scope. The whole script stops before running. Keep the first declaration and drop the keyword from the later one: write score = 10, not let score = 10. In a switch, wrap each case in { }.

  1. 1. Search for the nameSearch the file for the name in the message with let, const, class or function in front.
  2. 2. Reassign, do not redeclareRemove let or const from the second line, or rename it. Do not redeclare function parameters either.
  3. 3. Check switch and scriptsAdd { } around each switch case. Two classic script files on one page share their top-level names.

What does SyntaxError: Identifier 'x' has already been declared mean?

let and const create a name once. A second let or const for the same name in the same block (or the same function) is a syntax error, so the whole script stops before running anything. To change a value, assign without the keyword: x = 2, not let x = 2. Note that every switch case shares one block unless you add braces.

Why does SyntaxError: Identifier 'x' has already been declared happen?

1. Declaring a variable a second time

The second let is not needed. Just assign the new value.

Breaks

let score = 0;
// ...later
let score = 10;

Works

let score = 0;
// ...later
score = 10;

2. Redeclaring a function parameter

A parameter is already declared inside the function.

Breaks

function greet(name) {
  let name = name.trim();
  return "Hi " + name;
}

Works

function greet(name) {
  const clean = name.trim();
  return "Hi " + clean;
}

3. The same const in two switch cases

All cases are in one block. Wrap each case in { } to give it its own scope.

Breaks

switch (shape) {
  case "square":
    const area = s * s;
    break;
  case "circle":
    const area = 3.14 * r * r;
    break;
}

Works

switch (shape) {
  case "square": {
    const area = s * s;
    break;
  }
  case "circle": {
    const area = 3.14 * r * r;
    break;
  }
}

4. Two script files declaring the same top-level name

Classic <script> tags on one page share the same top-level scope, so const API in both files clashes.

Breaks

// a.js
const API = "/v1";
// b.js (same page)
const API = "/v2";

Works

// a.js
const API_V1 = "/v1";
// b.js
const API_V2 = "/v2";

How to find it in your own code

Checklist: 1) Search the file for the name in the message, looking for let, const, class or function before it. 2) Keep the first declaration and remove the keyword from the later ones (or rename one). 3) In switch statements, add { } around each case. 4) If two script files clash, rename one, or load them with type="module" so each file has its own scope.

Practise scope and closure bugs

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.