What is TypeError and ReferenceError?

What is TypeError and ReferenceError?

'ERRORS ARE BYPRODUCT OF CODING'

TypeError:

TypeError occurs when a value is not of an expected type. Let's see some Examples on TypeError

Cases of TypeError:

  1. When we try to reassign variables declared with const keyword TypeError will be thrown.

  2. Calling a non-function value as a function.

  3. An error occurs when a value is used outside the scope of its data type.

  4. While using an object that does not have the property or function that you are trying to access:

ReferenceError:

When we declare a variable compiler will automatically reserve memory for the declared variable. When we try to access the variable it will look for a reference of the variable when the reference is can't be found or if the variable is not declared it will throw ReferenceError.

Cases of ReferenceError:

  1. When we try to access a variable inside a function scope which is declared with let or const keyword globally it throws ReferenceError because it is not declared inside the scope but we are trying to access the variable inside the function scope. let and const are block scoped it can only be accessed within a block where it is declared at first. Based on this example it can be accessed globally after the function scope.

  1. When you try to access a variable before declaring the variable it will throw ReferenceError.

Conclusion -

  • TypeError in Javascript occurs when you try to use a value in a inappropriate way.

  • ReferenceError in Javascript occurs when you try to access a value in a inappropriate way