Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

errors
javascript
communitycreator

What are the 6 types of errors in JavaScript codes?

Theodore Kelechukwu Onyejiaku

JavaScript errors

In any programming language, errors occur. There are basically two types of errors in programming. These are:

  1. Program Error:
    This is an error encountered by the program that might require error handlers to manage. An example could be network disconnection, timeout error, HTTP response errors, etc.
  2. Developer Error:
    This is an error caused by the programmer. Examples include syntax errors, logical errors, semantic errors, etc.

The 6 error types in JavaScript

The following are the 6 most common error constructors in JavaScript:

Note: The error constructors below are invoking the Error() constructor function.

  1. Syntax error (SyntaxError): This is done when you use an already pre-defined syntax in the wrong way.

Run the code below to see a syntax error. It will be thrown because there are no closing braces ‘}’ for the function. We can see this here:

const ourFunction = () =>{
console.log(hello)
// Missign }
  1. Reference Error (ReferenceError): this occurs when a variable reference can’t be found or is not declared. Run the code below to see the type of error it logs:
console.log(unKnownVariable);
  1. Type Error (TypeError()): This is an error caused by misusing a value outside its own data type scope. We can represent this in the following way:
let number = 5;
console.log(number.split("")); //Converting a number to an array will throw an error
  1. Evaluation Error (EvalError()): This is no longer thrown by the current JavaScript Engine or EcmaScript Specification. However, it still exists for backward compatibility. It is called when you use the eval()backward function, as shown below:
try{
  throw new EvalError("'error in evaluation'")
}catch(error){
  console.log(error.name, error.message)
}
  1. RangeError (RangeError()): This error is caused when there is a need for an expected range of values, as shown below:
const adultChecker = (age)=>{
  if (age < 18) throw new RangeError("Not an adult!");
  return true
}

adultChecker(12);
  1. URI Error(URIError()): This is called whenever a wrong character(s) is used in any URI function. A sample of this is shown below:
console.log(decodeURI("https://www.educative.io/shoteditor"))

console.log(decodeURI("%sdfk"));  // Wrong URI URIError thrown

Conclusion

In JavaScript, errors can occur. Programs throw errors and developers can cause errors. As developers, we make sure we throw the particular error that should be thrown. You have now seen some of the error constructors that are present in JavaScript. Thank you!

RELATED TAGS

errors
javascript
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring