Account
Categories

What is an Error and its Types?


Definition:

When you write a program and run it,sometimes the code does not work.

It happens when a part of the program is incorrect.For example, a syntax error, a parenthesis error, a comma error, a logic error, etc. These problems cause errors, and the program will not run. It will also display an error message.

Some types of errors in JavaScript are:

1. Syntax Errors

Definition:

It is an error that happens when you make a mistake in writing the program, and some part of the code is missing. For example, if an if statement, parentheses, or other needed parts are missing. It is called a syntax error.

Example:

let x = ;

Output:

SyntaxError: Unexpected token ';.'

2 Reference Errors

Definition:

It is an error that occurs when you make a mistake in writing the program, meaning a reference used in your code does not exist or has no value. It is called the reference error.

Example:

console.log(value);

Output:

ReferenceError: value is not defined

3. Type Errors

Definition:

It is an error that may occur when you make a mistake in the program — that is, when you define a value but accidentally use it in a different way that does not match its correct type. It is called a type error.

Example:

let num = 5;
num.toUpperCase();

Output:

TypeError: num.toUpperCase is not a function

4. Runtime Errors

Definition:

It is an error that may occur when you run the program. Even if the program code is correct, an error can still present while the program is being executed line by line. It is called the runtime error.

Example:

let arr = [1, 2, 3];
console.log(arr[5].toString());

Output:

TypeError: Cannot read properties of undefined

5. Logic Errors

Definition:

It is an error that may occur when you run a program. You use the wrong method, formula, or idea that produces the logic error. The program runs, but the result is not correct.

Example:

let a = 10;
let b = 5;
let result = a - b;
console.log(result);

Output:

5