Curriculum Series

Reference vs Syntax Error

Reference vs Syntax Error

Reference vs Syntax Error

JavaScript

Syntax Error

A syntax error occurs when you write code that violates JavaScript's language rules - essentially invalid code that can't be parsed. The code won't even run.

JAVASCRIPT
1// Syntax Error examples
2if (x === 5 { // Missing closing parenthesis
3 console.log("Hello");
4
5let const = 5; // Using reserved keyword as variable

Reference Error

A reference error occurs when you try to use or access a variable or function that doesn't exist or is out of scope. The code is valid JavaScript syntax, but refers to something undefined.

JAVASCRIPT
1// Reference Error examples
2console.log(undefinedVariable); // Variable doesn't exist
3
4function test() {
5 let x = 5;
6}
7console.log(x); // x is not accessible outside the function
8
9nonExistentFunction(); // Function doesn't exist

Resources

Finished reading?

Mark this topic as solved to track your progress.