TypeError vs ReferenceError in JavaScript

TypeError vs ReferenceError in JavaScript

TypeError

Type error occurs when the operation you are trying to perform does not suit the variable’s data type. For Example, you can’t make integers uppercase:

In the above example, we have declared the variable num as a number data type. Attempting to convert it into uppercase results in TypeError.

Another scenario, in which we could get typeError is when trying to reassign value to a constant(const) variable.

TypeError occurs when the operation you are trying to perform is undefined(for the given data type) which means that, it is not defined by the rules of JavaScript.

Reference Error

Reference error occurs when we try to access a variable that does not yet exist (in the scope where you try to access).

In the above example, we try to perform an addition operation on a and b, which were not yet defined.

Therefore, it results in a reference error.

In short, a reference error occurs, when we try to perform operations on variables that are not defined yet.