Logic Error
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."
Related Terms
Bug
A bug is anything in your code that makes it behave wrong.
Debug
Debugging is the process of finding and fixing the gremlins in your code. Something is broken, and you need to play detective — adding clues (console.
Runtime Error
A runtime error is one that only shows up when your program is actually running, not before.
Syntax Error
A syntax error is when you write code that the computer cannot even understand — like handing someone a sentence with no verbs.