SyntaxError is caught at parse time (malformed code, const without initializer, re-declaration). ReferenceError is at runtime (variable does not exist or is in TDZ). TypeError is at runtime (value is wrong type for the operation, reassigning const).
SyntaxError vs ReferenceError vs TypeError in JavaScript
What is the difference between SyntaxError, ReferenceError, and TypeError in JavaScript?
The answer
More FAQs in SyntaxError vs ReferenceError vs TypeError in JavaScript
Because the engine catches it during parsing, before any code runs. 'const x;' is malformed syntax (const requires an initializer), so it is a SyntaxError, not a runtime ReferenceError.
Because the variable exists (no ReferenceError), but the operation (assignment) is not allowed for a const. The value's type (const binding) does not support reassignment, so it is a TypeError.
ReferenceError: Cannot access 'x' before initialization. The message is different from 'not defined', but it is still a ReferenceError because the variable reference cannot be resolved to a value.
Still have questions?
Browse all our FAQs or reach out to our support team
Want to upskill yourself?
Your learning library
Small steps. Big possibilities.
Find your next course. Build something great.