Skip to content

Unreachable Code

Quick summary

You wrote code after a return or break statement — it can never run and is dead code.

Why this happens

  • Code after a return statement in a function

  • Code after a break or continue in a loop

  • Code in a branch that can never be reached

Minimal example

✗ Broken code
function check(x) {
  if (x > 0) return "positive";
  return "non-positive";
  console.log("done"); // Unreachable!
}
✓ Fixed code
function check(x) {
  if (x > 0) return "positive";
  return "non-positive";
}

The console.log after the second return can never execute — both branches return before reaching it. Removing it eliminates the dead code.

How to diagnose

  • Look for code after return, break, or continue statements

  • Check if a linter warns about unreachable code

  • Verify that all code paths are reachable

How to fix

  • Remove the unreachable code

  • Move the return/break to after the code that should run

  • Restructure the logic so all code is reachable

How to prevent

  • Use a linter that detects unreachable code

  • Keep return statements at the end of functions when possible

Related Resources

Related Glossary

Related Lessons

← Back to language