ReferenceError  and TypeError

ReferenceError and TypeError

Errors in JavaScript are objects shown whenever a programming error occurs. These objects contain ample information about the type of error and the statement that caused the error. We will mainly discuss referenceError and typeError.

ReferenceError

The Javascript ReferenceError is thrown when an attempt is made to reference a non-existing or out of scope variable. This error occurs in either of the following cases:

  • Undefined variables - Not defining a variable before referencing it is one of the most common causes of ReferenceError in Javascript.

Example:

let firstName = "Mahima"
let age = 25

console.log(lastName)
// Uncaught ReferenceError: lastName is not defined
  • Out of scope variables - Variables defined inside a function's scope cannot be accessed outside of it. If an attempt is made to reference an out of scope variable, a ReferenceError is thrown.

    Example:

function name() {
    var str = "Mahima";
    return str
};

console.log(str);
// Uncaught ReferenceError: str is not defined
  • Strict mode - Using strict mode in Javascript can throw a ReferenceError if a variable is not defined using the var, let or const keyword
function referenceErr(a){
    "use strict";
   foo = true;

   if(a == 0){
     return foo
   }

  };

  console.log(referenceErr(1))
 // Uncaught ReferenceError: foo is not defined
  • Variable redeclarations - Redeclaring variables using the wrong keywords can also throw a ReferenceError.

    Example:

      function redeclarations() {
          let declare = 1;
          if (true) {
            let declare = (declare + 1);   
          }
        }
        console.log(redeclarations())
      // Uncaught ReferenceError: Cannot access 'declare' before initialization
    

    TypeError

    The TypeError occurs when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type. It occurs in following cases:

  • Attempting to call a function on an object that is not a function or not defined.

Example:

let obj = {
  name: "Mahima",
  age: 25
};

obj.sayHello();
//Uncaught TypeError: obj.sayHello is not a function

In this example, we're attempting to call a function named "sayHello" on the "obj" object using dot notation. However, obj does not have a property or method named "sayHello", so this code will result in a being thrown with the message "obj.sayHello is not a function".

  • When attempting to modify a value that cannot be changed.

    Example:

      const name = "hello";
      name = "world";
      // Uncaught TypeError: Assignment to constant variable
    

    In the above example, we are trying to reassign the value of the "name" variable which is declared using const. This is not allowed, and hence it results in a TypeError.

  • When attempting to use a value inappropriately.

    Example:

      let num = 24;
      let str = "hello";
    
      // Attempting to call a string method on a number
      let result = num.trim();
    
      // Output: Uncaught TypeError: num.trim is not a function
    

    In the given example, we are using the "trim()" method on a number "num". But the "trim()"method works on "string" only. Hence, we get the TypeError.

    Conclusion

In nutshell, we can say that the main difference between ReferenceError and TypeError is that a ReferenceError is thrown when you try to reference a variable that does not exist, while a TypeError is thrown when you try to operate on a value that is not of the expected type.