It’s very easy to make a mistake while coding. All you have to do is type one character wrong in a variable name and things won’t work. So let’s take a look at some of the common errors that you’ll run into, and it doesn’t matter if you’re a new programmer or you’ve been doing it for a long time, it’s important to hunt down these errors quickly and fix them quickly.
//Correct Code let productName = "Football"; console.log(productname); //Incorrect Code let productName = "Football"; console.log(productame);
In the above ‘correct‘ code, We are initializing productName variable and displaying its value in the console. It works fine without errors.
In the ‘incorrect‘ code, in the log function, we have passed the incorrect variable name (spelling mistake). If you run that code you will see ‘Uncaught ReferenceError: productame is not defined‘ error message.
//Incorrect code console.log(productName);
Suppose you have forgotten to declare a variable name and you are trying to get value from it than it will throw same error. So always remember to declare a variable before using it.
//Incorrect code let productName; console.log(productname);
In the above example, we have defined the variable but we haven’t initialised it. So when we try to display its value in the console, it doesn’t pop up with any error but shows value as ‘undefined‘.
//Incorrect code let class = "Football"; console.log(class);
In the above example, we have defined variable with class name. You cannot do this because class is a reserved keyword. You cannot use reserved words as a variable name.