×

Index

JavaScript Tutorial

JavaScript Examples

CSS Tutorial

CSS Examples

Errors in JavaScript

JavaScript Errors: Here, we are going to learn about the various types of the errors in JavaScript with examples.
Submitted by Siddhant Verma, on October 11, 2019

JavaScript Errors

Errors are very common in programming and development. Having errors in your program is a validation of your accuracy and an opportunity to strengthen your concepts and learn even more. Every language has a specification for errors. If your code does something unusual, it catches that unusual activity and shows an error. It's important to be able to understand errors only then one can remove it.

Types of Errors in JavaScript

There are 6 primary errors in JavaScript,

  1. EvalError:
    Raised when the eval() functions are used incorrectly.
  2. RangeError:
    Raised when a numeric variable exceeds its allowed range.
  3. ReferenceError:
    Raised when an invalid reference is used.
  4. SyntaxError:
    Raised when a syntax error occurs while parsing JavaScript code.
  5. TypeError:
    Raised when the type of a variable is not as expected.
  6. URIError:
    Raised when the encodeURI() or decodeURI() functions are used in an incorrect manner.

Reference, Syntax, and Type Errors

Reference Error, Syntax Error, and Type Error are the most common errors even to novice programmers and developers. Even experienced programmers encounter this error so let's look at some common examples of these errors, try to read and interpret the error and ultimately clean up our code leaving it error-free,

Example

function checkValue(value) {
    if (value > 3) console.log("Old value encountered");
    if (value < 0) console.log("Unacceptable value");
    else if (value > 0 && value < 3) console.log("New value obtained");
}

Before reading further or running this code on the console, carefully read the code line by line, top to bottom. We have made a very obvious syntax error. We ended our function with a ) instead of a }. Since this deviated from the natural grammar of JavaScript, invoking this function will give us a syntax error.

checkValue(1.7);

Output

Uncaught SyntaxError: Unexpected token ')'

Example

If you miss braces somewhere, use incorrect order of braces, or do something that is syntactically incorrect, you get a SyntaxError. Also, this error tells you what syntax you've spelled wrong. So all we need to identify is a ) and see where we have placed it, and modify it.

function checkValue(value) {
    if (value > 3)
        console.log('Old value encountered');
    if (value < 0)
        console.log('Unacceptable value');
    else if (value > 0 && value < 3)
        console.log('New value obtained');
}

checkValue(1.7);

Output

New Value obtained

Example

Look at the following code,

var num=3;
num.toUpperCase();

Output

Uncaught TypeError: num.toUpperCase is not a function 
at <anonymous>:1:5

Now we get a TypeError because the type of num should have been a character or a string, not a number. This is logically stupid though since someone who knows num is initialized a number would not assume it to be a string and try to convert it to upper case. However, if during your program you did something to convert num from a string to a number and then tried to add that method onto it, you will get the same error.

Example

Let's look at another example of TypeError,

var t = 'bananas';
Object.create(t);

Output

 Uncaught TypeError: Object prototype may only be an Object or null: bananas
    at Function.create (<anonymous>)
    at <anonymous>:1:8

We're trying to create an object out of a string hence we get a type error,

Example

var t={
	name: 'bananas',
	price: '60rupees'
}

Object.create(t);

Output

}__proto__: 
    age: 20
    name: "fuzzy"
    __proto__: Object

Now our code runs perfectly fine.

Let's look at in which situations we can get a ReferenceError.

    s.substring()

Output:

Uncaught ReferenceError: s is not defined
    at <anonymous>:1:1

Since, s is not defined we get a reference error as we're trying to make an invalid reference to s.

The Error Object

The errors we deal with are coded within the language and an important feature through which we can dive deeper into how they’re designed is the Error object. The Error constructor creates an error object. Whenever your code goes wrong somewhere, an instance of the same Error object is thrown.

Error;
ƒ Error() { [native code] }

Just like every other object, it also has properties and methods attached to it,

Error.name;
"Error"

You can also see this error object inside out by typing the following,

Error.prototype;
{name: "Error", message: "", constructor: ƒ, toString: ƒ}
    constructor: ƒ Error()
    message: ""
    name: "Error"
    toString: ƒ toString()
    __proto__: Object

Errors when exploited for unusual functioning of the program lead to bugs. We can use exception handling to identify errors and remove them. JavaScript also allows us to use the strict mode which is very helpful in identifying errors.

JavaScript Tutorial »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.