Javascript Errors

Javascript Errors

TypeError and ReferenceError

Table of contents

```"Nothing in this world is perfect...!"

This is applied to coding as well. We encounter various types of errors while building code. ReferenceError and TypeError are part of them. Through this blog, Let's understand what are they.

ReferenceError:

How does it feel when you go to give a job interview and after reaching the interview location you find out that the company for which you are here doesn’t even exist? You got angry and your mind will start throwing negative thoughts. The same happens with JavaScript too.

The Javascript ReferenceError is thrown when an attempt is made to reference a non-existing or out-of-scope variable. Not defining a variable or parameter before referencing it is one of the most common triggers for reference errors.

const textOne = 'omkar';
const textTwo = 'patil';
const joinedText = textOne + textThree;
//as textThree is not defined in the code it will give referenceError

TypeError:

The TypeError in javaScript occurs when the type of the value is not of the expected type. This may also occur in the following circumstances:

  • an operand or argument passed to a function is incompatible with the type expected by that operator or function

    for eg.

  •       const numberInput = 45
          const makingUpperCase = text => text.toUpperCase()
          makingUpperCase
          //This will give an error because the expected type of input is 'text' but we are giving 'number' as a input..
    

    case 1: Expected type: text*; Input type:* number

    case 2: Expected type: text; Input type: text (working fine now... )

  • when attempting to modify a value that cannot be changed

    for eg.

  •       const nameInput = "omkar"
          nameInput = "tanay"
          //This is giving error because we are trying to assign new value to constant variable.
    

Hope you understand both types of errors. Happy reading..!