Sitemap
Manish Virgat

Manish Virgat

A CSE student and a tech geek. Loves to attempt challenges. A rapacious reader, enthusiastic individual and an ardent learner.

Errors in JavaScript

3 min readFeb 13, 2023
Press enter or click to view image in full size

GeeksForGeeks defines error as “an illegal operation performed by the user which results in the abnormal working of the program.” In simpler words, it is referred to as the problems that occur in the functioning of any program that is usually caused by the programmer while making or writing that program. Usually, errors are found while the program is being compiled (compile-time errors) or being executed (run-time errors). Hence to ensure the smooth functioning and working of the program it is necessary to eradicate all errors. The process of identifying and fixing the errors is called debugging.

In JavaScript, the error is an object based on the Error object and normally contains a name and a message. It is by seeing this name and message only, we can identify the error and debug it easily.

In JavaScript, we have majorly 4 types of errors:

  1. Range Error:

A RangeError is thrown while trying to pass a value as an argument to a function that does not allow a range that includes the value.

Consider this piece of code:

let a = [];
a.length = -5;
console.log(a[0]);

The above code will return a Range error as length can never be negative for any array. The following sentence will be displayed on the console.

RangeError: Invalid array length

2. Reference Error:

The ReferenceError object represents an error when a variable that doesn't exist (or hasn't yet been initialized) in the current scope is referenced.

Consider this piece of code:

var a, b;
console.log(c);

In the above code, we have only declared variables a and b. But we are asking the program to give us the value of c, this will give us a reference error.

ReferenceError: c is not defined

3. Type Error:

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.

Get Manish Virgat’s stories in your inbox

Join Medium for free to get updates from this writer.

A TypeError may be thrown when:

  • an operand or argument passed to a function is incompatible with the type expected by that operator or function; or
  • when attempting to modify a value that cannot be changed; or
  • when attempting to use a value in an inappropriate way.

For example, look at the following code snippet:

var a= 100;
var b = a.toUpperCase();

toUpperCase() function is used to convert the elements of any string to uppercase elements. But since the variable a contains a number type value, it will throw an error in the console.

TypeError: a.toUpperCase is not a function

4. Syntax Error

The error that is caused by the incorrect use of a pre-defined syntax is called a Syntax error. The SyntaxError object represents an error when trying to interpret syntactically invalid code. It is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code. They are detected while compiling or parsing source code.

Consider the following piece of code for example:

var a=[1, 2, 3, 4, 5];
console.log(a[1);

On running the above code, we will get a message on the console:

SyntaxError: Unexpected token ‘)’

Here we opened the square bracket to enter the position of the element we want to get, but we forgot to close it, hence this will throw an error.

The actual code should be:

var a=[1, 2, 3, 4, 5];
console.log(a[1]);

Further Reading: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error

Conclusion:

Errors are an integral part of any program writing process, it is almost impossible to write a big chunk of code without any errors in it. It is thus important to understand the different types of errors and ways to debug them in order to make our program run efficiently and get desired output.

--

--

Manish Virgat
Manish Virgat

Written by Manish Virgat

A CSE student and a tech geek. Loves to attempt challenges. A rapacious reader, enthusiastic individual and an ardent learner.