Bhakti Kharatkar
Bhakti Kharatkar's Blog

Bhakti Kharatkar's Blog

Types of errors in javascript.

Types of errors in javascript.

Bhakti Kharatkar's photo
Bhakti Kharatkar
·May 16, 2022·

3 min read

Table of contents

  • What is an error ?

Hello everyone, I hope you all are doing great. In this blog we will discuss about different errors in javascript and what they mean. so, let's get started.

What is an error ?

Errors are statements that don't let the program run properly. There are three main types of errors that can occur while compiling a JavaScript program:

  • syntax errors : This error occurs when there is something incorrect in the syntax of the program.
  • runtime errors : This error occurs during the runtime of the program after it's interpreted by the compiler.
  • logical errors : This error occurs when the syntax is correct but the logic is not.

Let us explore types of errors :

SyntaxError

  • This is most common error occured.
  • This error occurs when we use already predefined syntax incorrectly.
  • When syntax is invalid, javascript engine can't understand it and thus it rejects it upfront. The later code will not execute then.
  • This error occurs at interpret time in javascript.
  • for example

11.05.2022_21.04.32_REC.png

here 's' is the unexpected identifier.

TypeError

  • This error occurs when the value does not turns out to be of a particular expected data type.
  • Means when method is called or reads a property on wrong data type or undefined object.
  • State is not initialize properly when UI is rendered, this could be one of the reason. This leads to unsuccessful operation
  • This error also occurs when a variable is used outside its own data type scope. Following are some examples of typeErrors :

y.png

ReferenceError

  • This error is received when reference of the variable cannot be found or variable is used outside the current scope.
  • Also this error is received when you use a variable which is not declared.
  • Whenever we try to use a variable during temporal dead zone, this error is received. Temporal dead zone is a time period between when the variable is hoisted and it is assigned with some value.

yy.png

  • In the first example 'a' was not defined.
  • And in the second example we tried to access this 'name' in its Temporal dead zone i.e, it was hoisted but not initialize with some value, thus this error occurs. To avoid this error keep all the variable and function deaclration at the top of the program. like this

yyy.png

RangeError

  • This error occurs when we try to set a variable outside its defined range and when we pass a value to a function that is out of range.
  • Or when we call a recursive function that does not terminate.

yyyy.png

URIError

  • URIError (Uniform Resource Indicator) is thrown when there is a problem in encoding and decoding the URI.
  • URI in JS have functions : decodeURI, decodeURIComponent, etc. If we use any of them with the wrong parameter we will get a URIError.
  • Here decodeURI takes encoded URI and if it containes any invalid character is throws an error.

16.05.2022_09.04.34_.png

EvalError

  • An EvalError occurs when an error occurs with an eval() function call.
  • This error is no longer thrown by the current JavaScript Engine or EcmaScript Specification. However, it still exists for backward compatibility.

InternalError

  • This error occurs when there is a sudden spike of workload on the JavaScript engine.
  • The spike in workload happens when there is too much data to handle.
  • This occurs when the JS engine is overwhelmed by too many recursions and switch cases, etc.

EvalError and InternalError are not thrown in modern JavaScript.

I hope now you understand what these errors actually mean. Share your feedback in the comment about the blog. Have a nice day!

 
Share this