Skip to content

Logic Error

Easy — everyone uses thisGeneral Dev

ELI5 — The Vibe Check

A logic error is the sneakiest kind of bug — the code runs perfectly fine, no crashes, no errors, but it does the WRONG thing. Like a calculator that works great but always gives you the wrong answer. The computer did exactly what you told it to do; you just told it the wrong thing.

Real Talk

A logic error is a defect in a program's algorithm or control flow that causes incorrect behavior without producing exceptions or syntax errors. The program executes successfully but produces wrong output. These are the hardest bugs to find because no error is thrown — only careful testing, comparison with expected values, or code review can detect them.

Show Me The Code

// Logic error: wrong formula for average
function average(arr) {
  let sum = 0;
  for (let i = 0; i <= arr.length; i++) { // BUG: should be i < arr.length
    sum += arr[i];
  }
  return sum / arr.length; // includes undefined, result is NaN
}

// Looks fine, runs fine, but returns wrong result
console.log(average([2, 4, 6])); // NaN instead of 4

When You'll Hear This

"No errors, but it's showing the wrong total — that's a logic error." / "Logic errors are why you need tests."

Made with passive-aggressive love by manoga.digital. Powered by Claude.