Programming is often compared to magic, but sadly, there is no magic in programming languages. We tell the computer what to do through languages that are deterministic. For every result there is a reason, and for every error there is a logical cause. Making, encountering, and working with errors are just part of the game.
(This is not an error, but we often see this and treat it like an error, so it’s helpful to know what it means)
Undefined value primitive value is used when a variable has not been assigned a value. Undefined type is a type whose sole value is the undefined value.
The standard states that you will encounter an undefined value when accessing uninitialised/unassigned variables and non-existing object properties, elements and alike, or the reference to a variable or object is made outside the current scope of execution.
A variable declaration that has not been assigned to any value (uninitialised) assumes the undefined value. A function without a return statement, or with an empty return statement also returns undefined. For instance:
“myArray ”variable isn’t defined anywhere, and we need to make sure that the datatype will either be a string or array.
What’s an “unresolvable reference”?
According to ECMA standards, a Reference type consists of three components — base value, reference name, and strict flag. (Read more about this here) When a Reference is created, its components are set to some values.
For variable-based References, the base value is the variable object — an object related with an execution context that stores and declares in the context: variables and functions. The base value is the variable object of the current execution context, and each functional context has its own abstract variable object. Therefore, a Reference is unresolvable if its base value is undefined.
When an Identifier — “bat” can’t be found anywhere in the scope chain, a Reference will be returned with the base value set to undefined, thus throwing a ReferenceError.
line 4 should return a ReferenceError, but it throws a TypeError instead, because the base value is subject to CheckObjectCoercible. It throws a TypeError when trying to convert undefined to an Object. The Reference will never evaluate as unresolvable, as long as the var keyword ensures that a variable object is assigned to the base value.
A TypeError is returned when an argument operand is passed into a function and evaluates a value that is not the expected type. Using the wrong data type for a certain operations/methods will result in a TypeError.
As seen above, the declaration of variable “c” will by default evaluate to undefined , which itself is its own datatype that cannot be invoked like a function call. Undefined cannot be converted into a function by invocation, therefore will result in a TypeError.
The .toUpperCase method is part of the String.prototype, and therefore cannot be used on a Number. You cannot call the string value of “num”.
Errors are useful when you know what they mean. Understanding what type of error the interpreter is logging out will save you time when debugging through your code. With that said, error handling is a skill that is very important to know. A lot of programmers are not quite sure how to handle errors in the most productive way, which leads to haphazard error handling. While there are many more common errors that we run to on a daily basis, I’m curious as to know what your thoughts on the importance of error handling are?
I’ve shared some resources below that are worth checking out: