What are the different types of errors in JavaScript?
JavaScriptThe 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 bracketconst obj = { name: 'John'; // SyntaxError// Invalid syntaxlet 123abc = 'hello'; // SyntaxErrorThese 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 TDZconsole.log(x); // ReferenceErrorlet 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 nullundefined.map(); // TypeError: undefined is not a functionconst num = 42;num(); // TypeError: num is not a functionThis 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 100Custom 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.