Javascript Errors
Table of contents
Javascript is a high-level programming language for the web. It is widely popular and helps in creating dynamic and interactive web content.
But it is also prone to errors and bugs like any other language. This blog will discuss a few types of errors that occur in javascript.
Errors
The error occurs when we don't write proper syntax. Errors often remain undetected till the program is parsed. When the compiler runs the program this error inhibits the program from getting compiled or executed and throws an error.
There are quite a few errors present in javascript. Here we are going to discuss 4 of the most common ones that we face in our daily lives.
Syntax Error
This error occurs when we break the rules of javascript language and type incorrect predefined syntax or semantically invalid code.
These are detected while compiling or parsing the source code that's why it's also called parsing errors.
For example, let's look at this screenshot.
Here we are getting a syntax error. Why? Can you guess it? It's because we forgot to put curly braces after starting the function that is after "fruit()". So by correcting it will look like this.
The compiler won't understand that it's a function if the syntax is not correct.
Type Error
This error occurs when we misspelled a word or when a variable exists but the operation performed on it is a value that is not of the expected type.
For example, declaring a variable and calling a function with that variable name. We can't do that with a variable cause it's not a function so we cannot call it.
The correct format will be. Creating a function and then calling it.
Reference Error
As the name suggests this error occurs when an invalid reference is used.
For example, you try to use a variable that is not defined or doesn't exist at all.
Here in the screenshot, we are trying to console log the value of the variable apple which hasn't been declared.
The correct way will be first declaring it and then console logging it.
Range Error
This error occurs when a number that is outside an allowable range of values is passed into a method.
For example, let's take the toFixed() method to understand it.
As shown in the error there is a precision argument that is out of range which is (-20). It should have been between 0 to 100. So correct way is to use a range in between the predefined range.
So that's all about the errors in javascript. Hope I was able to clear some doubt.
See you soon. Bbye ๐.
Subscribe to my newsletter
Read articles from Prachi Sahu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by