Type error, Undefined. Most common mistakes in Javascript and how to resolve them.

Javascript is almost everywhere on web. But sometimes we do make mistakes in writing the code. Here we will see how to solve them.

How to debug or see the errors

Press F12 in browser it will open developer tool. There click on console tab. Here you can see warnings and errors or the log statements that are part of your Javascript code. You can choose on what to see in errrors/warnings/log statements etc.

Undefined variable

If you are getting Type error undefined most often it is the variable its referring to. It is either not storing a value or is not declared as var.

User input validation

Make sure to remove extra white spaces using Javascript’s trim() function.

If your if condition variable is not matching as expected

Then make sure your variable’s type is correct. That is sometimes you might be matching a string but you might be storing it as a integer/float. Just print the type of the variable to see what is happening.