Many times as programmers, we will stumble on errors. The bright red letters that appear may seem intimidating at first, especially if you don’t know where you went wrong. Seeing so much red may be alarming especially, for new folks and, many teachers may tell you to search up the error message but, what does that mean? What may help is if you know what you are looking for. In this blog, we will discuss the different types of error messages and how to fix the errors!
If your console.log throws an error that reads: Uncaught ReferenceError, this is most likely due to variable problems. If you defined a variable within a block scope and you try to console.log it outside of its scope, this is the error that will be thrown. Also, if you console.log a variable that doesn’t exist, then this error will be thrown as well.
An easy example to demonstrate this error is this:
Let hello = “Hi”;
You may have been typing really fast and accidentally misspelled your variable name. Don’t fret if you see this error type — go back into your code and check your variables. If all of your variables are correct and you are still getting this error, you need to go back and check your variables with a scope lens. A great question to ask yourself is, can I reference this variable out here? Is it out of its scope?
The syntax is the grammar of code and, if the grammar is wrong, then you would get Uncaught SyntaxError thrown as an error. An example of what may cause this error is:
Let hello = “Hi;
A SyntaxError may consist of a missing parenthesis or like in the example above a missing opening or closing quotation mark. When a syntax error is thrown at you, don’t fret. Look back at your code and check if for every opening quote there is a closing quote and if for every opening parenthesis there is a closing one and vise versa.
Another reason why you may get a SyntaxError thrown is if you try to reassign or redeclare a variable with the keyword const within the same scope. With the keyword let, you can reassign but not redeclare. If you try to redeclare a variable with the keyword let within the same scope, a SyntaxError will be thrown. Check your variable declarations and reassignments and make sure you are using the correct keywords if this error keeps popping up.
TypeErrors occur when a variable or parameter is not a valid type. This error is thrown when you try invoking something that isn’t a function! Another reason why this error may be thrown is when trying to reassign something that can’t be reassigned. A good example of something that can’t be reassigned is the variable declaration keyword const. Reminder: const can’t be reassigned within the same scope. This is an example of TypeError due to reassignment:
const hello = 'hi';
hello = "Bonjour";
In this example, the variable hello can not be reassigned. A good rule of thumb is: if you are going to change the variable or reassign at any point in your code, initialize it with the keyword let.
As a programmer, you will run into a lot of errors. Understanding the error message will begin to become second nature. Something to remember is that it doesn’t matter how many errors you come across. What is important is not giving up and fixing the errors. A good tip is not to freak out when you get an error. Instead of freaking out, read the message, take a deep breath, and go back into your code. Use Google! If you don’t know what the error message is trying to tell you, search it up. Also, check on your needs. If you find yourself making many small, mistakes it could indicate that you need to step away from your code for a bit.