Skip to main content

Syntax error VS runtime error

1. What a syntax error is

A syntax error (SyntaxError) is an error that occurs before the program even starts running, when JavaScript cannot parse the code because the language's syntax rules are violated.

Simply put: the code cannot even begin to run.

Examples of syntax errors

javascript
if (true { // missing parenthesis console.log("ok"); }
javascript
function() { // a function without a name console.log("hi"); }
javascript
const a = ; // no value after "="

The interpreter detects such errors at the parsing stage (before execution), so the code does not run at all.

An example with an explanation

javascript
console.log("before the error"); eval("let x = ;"); // SyntaxError console.log("after the error"); // will not run

The error occurs immediately when trying to interpret the string, so the program never reaches the second console.log.

2. What a runtime error is

A runtime error is an error that occurs while the program is running, after the code has already passed syntax checking successfully.

That is, the syntax is correct, but the program's logic leads to a failure during execution.

Examples of runtime errors

javascript
console.log(a); // ReferenceError: a is not defined
javascript
let obj = null; obj.name; // TypeError: Cannot read properties of null
javascript
JSON.parse("invalid JSON"); // SyntaxError, but raised during execution

In these cases the code runs, but breaks at a specific line during execution.

3. The main difference

CriterionSyntax errorRuntime error
When it occursAt the parsing stage (before the code runs)While the program is running
Can it be caught with try...catchNo (the code will not even start)Yes, it can be handled
Exampleif (true {console.log(a)
JS reactionThe code does not run at allExecution stops at the error
Error typeSyntaxErrorReferenceError, TypeError, RangeError, etc.

4. An example for a clear comparison

javascript
// Syntax error: try { eval("if (true { console.log('hi') }"); // <-- invalid syntax } catch (e) { console.log("Caught?", e.message); } // → this code will not even run, because the error occurs before execution // Runtime error: try { console.log(a); // ReferenceError } catch (e) { console.log("Caught:", e.message); }

Summary

TermWhat it means
SyntaxErrorAn error in the code's structure - JS cannot interpret it
Runtime ErrorAn error while running syntactically correct code
DifferenceThe first happens at the parsing stage, the second at the execution stage

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.