What are errors in JavaScript?

errors are encountered by programmers every day when writing. They are a crucial part of developers' life. Therefore understanding what is an error and what different types of error mean is very handy knowledge.

What is an error?

Errors in Javascript describe any issue that happens during the execution of code. They can be of different types. Error is represented as an object in red text with an error type and a message of what went wrong.

Let us see a few common error types.

Common error types

Syntax Error

We have certain rules that need to be followed that make up the JavaScript language construct. these are called syntax. Not following this syntax will lead to SyntaxError.

Syntax error happens during the parsing phase when our code is tokenized and parsed.

In the above example, we are trying to declare a const variable without an initial value. This is against the rules of JavaScript so we get a syntax error.

Type Error

TypeError happens when we try to operate on the wrong type. For example, trying to use spilt("") on a boolean value will give TypeError.

Reference Error

reference error represents when we try to access a variable that does not exist or is not initialized. In the below example, we are trying to access the variable okay before it is declared. Since it is declared with let we cannot access the value of the variable before it is declared.

Conclusion

We saw above what is an error and a few common errors. For more details about errors and their different types please check out the below link:

Errors MDN