DEV Community

Zipy team for Zipy

Posted on • Originally published at zipy.ai

6 Everyday Javascript Errors

everyday javascript errors

Javascript is a programming language that is used in many websites and applications. However, like all programming languages, JavaScript can have errors. These errors can be challenging to find and fix, but it is crucial to do so to maintain the functionality of your website or application.

Most developers try to avoid errors as much as possible, but here at Zipy, our entire product is built around capturing and managing mistakes. We deal with many ins and outs and manage various JavaScript errors and their related APIs.

Let's look at the most common JavaScript errors that developers face and how you can solve or prevent them from happening.

Types of JavaScript errors

1. Type error

Type errors in JavaScript are pretty common, especially when you're just starting out with the language. Broadly, there are two types of type errors that you'll encounter:

1. Using a value of the wrong type: This is the most common type of type error. It happens when you try to use a value as if it were of a different type. For example, you might try to use a string as if it were a number.
2. Trying to use a value that doesn't exist: This type of Javascript error happens when you try to use a value that doesn't exist. For example, you might try to access a property of an object that doesn't exist:

  • var myObj = {};
  • console.log(myObj.x);
  • In this case, you could check to see if the property exists before trying to use it.

2. Syntax error

When it comes to programming, syntax refers to the rules that govern the structure of the code we write. In JavaScript, syntax errors occur when the parser encounters code it doesn't understand - usually due to typos or keyword misuse.

Other common syntax errors include using a reserved word as a variable name (for example, trying to use var default = "foo"; would produce a mistake because "default" is a reserved word in JavaScript) or forgetting to close parenthesis or curly brace.

For example:

if (true) {
    console.log("Hello World");
}
Enter fullscreen mode Exit fullscreen mode

Finally, another common JavaScript error is forgetting to include semicolons properly. In JavaScript, semicolons are used to terminate statements. If a programmer fails to include a semicolon, it can cause the code not to work properly.

3. Reference error

A Reference Error occurs when you try to access a variable that does not exist. When a Reference Error occurs, the variable does not exist in the current scope.

For example:

var x = y + z
Enter fullscreen mode Exit fullscreen mode

There are two ways to fix a Reference Error. The first is to ensure that the variable exists in the current scope. The second is to use the strict mode. Strict mode is a new feature in JavaScript that prevents Reference Errors from happening.

4. Range error

One common type of range error is when a value is outside the range of values that a particular data type can represent. For example, an integer can only store values between -2147483648 and 2147483647. If a value outside this range is stored in an integer variable, it will result in a range error.

A different type of range error can occur when a calculation results in a value outside the range of values that the data type can represent. For example, if an integer variable is divided by 0, it will result in a range error.

5. URI errors

URI (Uniform Resource Indicator) in JS has the functions: decodeURI, decodeURIComponent, etc.

One of the most common Javascript error types you'll come across is the URIError. This error is thrown when a malformed URI is encountered or when a URI is decoded that is not valid.

For example, if you try to decode the following URI:

https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Djavascript%253A%2520void(0)
Enter fullscreen mode Exit fullscreen mode

You'll get a URIError because the %3F in the query string is not a valid character.

6. Evaluation error

Eval Errors or evaluation errors are related to the global eval() function. Today's JavaScript engine no longer throws them, but they still exist for backward compatibility.

try {
    throw new EvalError("error in evaluation");
} catch (error) {
    console.log(error.name, error.message);
}
Enter fullscreen mode Exit fullscreen mode

Try/Catch blocks

JavaScript provides a statement called "try...catch" to deal with exceptions. When an exception occurs in the "try" block, the code in the "catch" block is executed. Exceptions can be thrown (or re-thrown) in the "catch" block.

The "try...catch" statement consists of two parts, the "try" block and the "catch" block: The "try" block contains code that may throw an exception. The "catch" block contains code that executes if an exception is thrown in the "try" block.

If no exception is thrown in the "try" block, the "catch" block is skipped. The "catch" block takes an argument, which is the exception object. The exception object contains information about the error, including the name of the type of JavaScript error, the message, the file name, and the line number where the error occurred.

Wrapping up

Javascript is a powerful language that can be used to create various applications. However, like any other language, it is essential to be aware of the different types of errors. Understanding the different JavaScript errors makes debugging code easier and ensures it works as intended. It's important to have visibility into errors that affect your users, and to have good tools to solve them quickly.

Zipy can help you with this task and make it infinitely easier. For example, our advanced dev tools find the root of a bug in seconds and tell you how and why the bug popped up on the user's browser.

Top comments (0)