Logic error

A logic error is a mistake in what a program has been told to do: the code is valid, runs to the end without crashing and gives a wrong result. Nothing stops and no message appears, so a logic error is found only by comparing what the program produced with what it should have produced.

The longer version

MDN's guide to troubleshooting JavaScript describes logic errors as errors "where the syntax is actually correct, but the code is not what you intended it to be, meaning that the program runs successfully but gives incorrect results". The computer did exactly what was written, and what was written was not what was meant. A free-delivery rule coded as more than 50 pounds when the policy says 50 pounds or more is a logic error. So is a monthly report that leaves out the last day of the month, and a list sorted as text so that 10 comes before 9.

Three kinds of error are commonly set side by side. A syntax error breaks the grammar of the language, such as a missing bracket, and the code is rejected before it does anything. A runtime error is valid code that fails while it runs, for example by reading a property of a value that does not exist, and in a browser it surfaces as an uncaught exception with a file and a line number. A logic error is neither. The language is satisfied and nothing fails, which is why MDN calls logic errors often harder to fix than syntax errors: "there usually isn't an error message to direct you to the source of the error".

The console usually stays empty, because the browser has no idea what the right answer was. A total of 47.50 is as valid a number as 45.00. The evidence therefore sits outside the program: the value that appeared, the value that was expected, and the exact inputs that produced the difference, such as the basket, the account or the date. A screenshot of the wrong figure with those inputs gives a developer somewhere to start. In MDN's tutorial the example is a number-guessing game that always picks 1, and the cause is found by logging the variable at each point where it is set. Under types of software bugs this is the logic bug, and like any software bug it can wait unnoticed for the one input the rule gets wrong.

Logic errors repeat a small number of patterns. Wikipedia's example is a C function meant to average two numbers that returns a + b / 2: division binds more tightly than addition, so the code halves b and then adds a. Others are the off-by-one loop that stops one item early or runs one too far, a comparison written as == where != was meant, a loop whose exit condition is never met, and a value of the wrong type. LaunchCode's tutorial shows the last one: a program converting 2 hours to minutes printed a long row of 2s in place of 120, because the input was still text and multiplying text repeats it. When the faulty rule guards money or access, the same kind of mistake becomes a business logic flaw. SecureFlag lists a negative quantity that lowers a price, a single-use discount code that works twice and a checkout that lets the payment step be skipped.

Common questions

What is a logic error in programming?

A logic error in programming is a fault in the reasoning of the code: the program is valid, runs without stopping and returns a wrong answer. Using the wrong comparison, the wrong order of operations or the wrong variable are typical causes.

What is the difference between a logic error and a syntax error?

A syntax error breaks the rules of the language, so the code is rejected and an error message says where. A logic error obeys every rule of the language and still does the wrong thing, so no tool objects and the program runs. Syntax errors are found by the parser, and logic errors by someone checking the result.

What is the difference between a logic error and a runtime error?

A runtime error stops the code while it is running, for instance when it reads a property of an undefined value, and it leaves an exception with a stack trace. A logic error stops nothing. The code completes and hands back a result that is wrong, with no trace to follow.

Why does a logic error not appear in the console?

The console records failures the runtime can recognise: exceptions, rejected promises and resources that did not load. A wrong result is none of those. The code ran as written and returned a valid value, so the runtime has nothing to report, and the fault shows only when the output is compared with the expected one.

How do you find a logic error?

Start from a case with a known right answer and compare it with what the program returns. Then narrow the gap: log or inspect intermediate values, step through the code in a debugger, and write a test that asserts the expected result for the failing input. The test stays in the suite afterwards and guards the fix.

What is an example of a logic error?

A C function that averages two numbers with return a + b / 2 is a standard example. It compiles and runs, but division happens before addition, so averaging 4 and 6 returns 7 in place of 5. The fix is the brackets the programmer meant: (a + b) / 2.

What is the difference between a logic error and a semantic error?

The two terms overlap and courses draw the line in different places. Often a semantic error is code that is grammatical but meaningless to the language, such as adding a number to a value of an incompatible type, so a compiler or interpreter can still object. A logic error is valid, meaningful code that does the wrong thing, which no tool can flag. Some sources treat the two names as one.

What are the main types of errors in programming?

The three usually taught are syntax errors, runtime errors and logic errors. Longer lists split these further, for instance by separating compile-time errors from syntax errors or by adding semantic errors. Of all of them, only the logic error lets the program finish normally with a wrong answer and no message.

Bug reports that arrive ready to triage.

Collect replay, console, network and browser context with the Free plan.

<script src="…/widget.js" data-key="pb_pub_…">