Before identifying and fixing these errors, let's clarify what an error is and its syntax.
Properties of an Error
- Message: A string value carrying the error message.
- Name: The type of error that occurred (very useful when googling looking for answers)
Some of the most common reasons why this can occur are:
- Missing quotes.
- Lack of closing parentheses
- Incorrect alignment of braces or other characters
It is good practice to use a linting tool in your IDE to identify these errors before they reach the browser.
When some value does not happen to be of a certain expected type, this error will pop-up. Some of the most common cases in which it occurs are:
- Calling objects that are not methods.
- Attempting to access properties of null or undefined objects.
- Using a string as a number or vice versa.
A RangeError occurs when a variable is set to a value outside of its legal value range. It usually happens when a value is passed as an argument to a function, and the given value is not in the range of the function's parameters. This can sometimes be difficult to fix when using poorly documented third-party libraries, since you need to know the range of possible values for the arguments to pass the correct value.
Some of the most common situations in which the RangeError occurs are:
- Attempting to create an array of illegal lengths using the Array constructor.
- Passing wrong values to numeric methods like toExponential(), toPrecision(), toFixed(), etc.
- Passing illegal values to string functions like normalize().
Occurs when something is wrong with a variable reference in your code. You may have forgotten to define a value for the variable before using it, or you may be trying to use an inaccessible variable in your code. In either case, reviewing the stack trace provides ample information for finding and fixing the broken variable reference.
Some of the most common reasons for reference errors are:
- Attempting to access block-scoped variables outside of their scope.
- Reference a global variable from an external library (such as jQuery's $) before it is loaded.
If you are interested in an extensive errors list, check out: Errors Docs -Developer.mozilla.org