What are the different types of errors in JavaScript?

The short answer

JavaScript has several built-in error types: SyntaxError (invalid code), ReferenceError (undefined variable), TypeError (wrong type), RangeError (value out of range), URIError (bad URI), and EvalError. You can also create custom errors by extending the Error class.

The common error types

SyntaxError: the code is not valid JavaScript. The engine cannot parse it:

// Missing closing bracket
const obj = { name: 'John'; // SyntaxError
// Invalid syntax
let 123abc = 'hello'; // SyntaxError

These are caught before the code runs, during parsing.

ReferenceError: you are using a variable that does not exist:

console.log(myVariable); // ReferenceError: myVariable is not defined
// Also happens with TDZ
console.log(x); // ReferenceError
let x = 10;

TypeError: you are using a value in a way that is not allowed for its type:

null.toString(); // TypeError: Cannot read properties of null
undefined.map(); // TypeError: Cannot read properties
// of undefined (reading 'map')
const num = 42;
num(); // TypeError: num is not a function

This is the most common error type you will see in JavaScript.

RangeError: a value is outside the allowed range:

new Array(-1); // RangeError: Invalid array length
(1).toFixed(200); // RangeError: toFixed() digits argument must be between 0 and 100

SyntaxError vs ReferenceError

The difference is when the error is thrown. A SyntaxError is thrown while the engine parses the script, so nothing in that script runs, not even the lines before the mistake. A ReferenceError is thrown at runtime, when the engine reaches a line that uses a name it cannot find, so every line before it has already run.

console.log('start'); // never runs
let const = 5; // SyntaxError: Unexpected token 'const'
console.log('start'); // logs "start"
function test() {
let x = 5;
}
test();
console.log(x); // ReferenceError: x is not defined

The second snippet is valid syntax. It fails only when the last line runs, because x exists inside test and is out of scope outside it.

Custom errors

You can create your own error types for better error handling:

class ValidationError extends Error {
constructor(field, message) {
super(message);
this.name = 'ValidationError';
this.field = field;
}
}
function validateAge(age) {
if (age < 0 || age > 150) {
throw new ValidationError(
'age',
'Age must be between 0 and 150'
);
}
}
try {
validateAge(-5);
} catch (error) {
if (error instanceof ValidationError) {
console.log(`${error.field}: ${error.message}`);
}
}

Interview Tip

Focus on the three most common types: TypeError, ReferenceError, and SyntaxError. Give a one-line example for each. If the interviewer asks about custom errors, show the extends Error pattern. Knowing when each type occurs shows you can debug errors quickly.

Why interviewers ask this

This tests basic JavaScript knowledge. Interviewers want to see if you can identify what kind of error you are dealing with, which helps in debugging. Knowing the difference between a TypeError and a ReferenceError means you can find the root cause faster.