TypeError and ReferenceError in Javascript
In this article, we will learn about TypeError and ReferenceError.
Table of contents
It’s not a pretty sight when an application dies😐. Error messages can be difficult to understand, and we sometimes have to put our investigator hats🕵️ on to solve the mystery and find the culprit🤫. The first step😌 to understanding JavaScript errors is to know where they come from.
In programming, an error is a section or part of code that breaks the normal flow of the program. In any programming language, there are usually three kinds of errors that one can face,
Compile Time Errors
Logical Errors
Run-Time Errors.
The only way we can debug our JavaScript is by utilizing the errors and warnings the best we can and finding the reason behind them.
so let's find the reason then😉
JavaScript error types
JavaScript has six types of errors that may arise during the script execution:
EvalError
RangeError
ReferenceError
SyntaxError
TypeError
URIError
Here we will see the TypeError and ReferenceError
1. TypeError 🤠
TypeError: Cannot read undefined properties.
The error clearly says that it is Undefined, which means the variable might be declared or used. Still, there is no value associated with the variable. In short, the value is not assigned.
In JavaScript, properties or functions are Objects, but undefined is not an object type. If you call the function or property on a such variable, you get the error in the console TypeError: Cannot read undefined properties.
Example of Type Error
Let’s understand the TypeError: Cannot read property of undefined
function someFunc(y) {
console.log(y);
}
var someVar;
someFunc(someVar);
Consider the above example,
var someVar: we have declared the someVar, but there is no value associated with it.
someFunc(someVar): This function call passes the someVar to the someFunc. In our case, the variable someVar doesn’t have any value as we have not assigned it, so by default, it’s undefined.
console.log(y): Inside the someFunc(), we have console.log, which is trying to access the value of y that belongs to someVar or y. Since the y is undefined, now if you execute this code, you will get the message TypeError: Cannot read properties of undefined (reading ‘x’)
The TypeError occurs when a variable or parameter is not of a valid type.
Common Scenarios:
Invoking objects that are not methods
Attempting to access properties of null or undefined objects
Treating a number as a string or vice versa
2.ReferenceError🤓
JavaScript Reference error is quite a common error that people encounter (especially beginners)😄.
This error simply means that there exists such a line in the code that is telling the compiler to access a variable or an object that has no memory address or location😯. Such a scenario happens when the variable in focus here is not yet initialized or declared at all.
If the variable is not declared, then it will not take up a memory location or address. By this statement, it is easy to conclude that this “ReferenceError – Variable is not Defined” occurs when the variable that the programmer is trying to access has not been previously declared before the statement that caused the error.
The ReferenceError
raises when you reference a variable, a function, or an object that does not exist.
See the following example:
In a case where a variable reference can't be found or hasn't been declared, then a Reference error occurs.
console.log(x);
Output:
console.log(x);
^
ReferenceError: x is not defined
Conclusion🥳
TypeError — my number two as far as an occurrence in development is concerned. Thrown when we have some value and it is not of a proper type.
ReferenceError — thrown when we want to refer to some variable that doesn’t exist.
That's it for the day hope you have enjoyed this blog.....
Thanks for reading😎.