You will make mistakes and your Programs will have errors. That's life.πŸ˜ƒ

You will make mistakes and your Programs will have errors. That's life.πŸ˜ƒ

Β·

3 min read

Common JavaScript Errors and How to Properly Deal With Them.

Errors can be caused by syntax mistakes, incorrect logic and other coding issues. In that unexpected situation, the program collects details about the error and informs the user that it cannot proceed.

3 types of errors

  1. Syntax Error

  2. Reference Error

  3. Type Error

Syntax Error

Each programming language has its own set of rules and way of writing the program. If the programmer violates any of these rules this is a syntax error. If you type console,log console comma log instead of console dot log, JavaScript will say "I have NO idea what you mean".πŸ˜ƒ

// Unmatched Parentheses
for (i = 0; i < 10; i++))
{
  console.log(i);
}

OutPut
// for (i = 0; i < 10; i++))
//                        ^
// SyntaxError: Unexpected token ')'
// Missing Comma
const fruits =  ["🍎", "πŸ₯" "πŸ‡"];
console.log(fruits);

OutPut
// const fruits =  ["🍎", "πŸ₯" "πŸ‡"];
//                            ^^^^
// SyntaxError: Unexpected string

Syntax error is like shouting nonsense. Nobody understands you.


Reference Error

In JavaScript, a reference error is thrown when a code attempts to reference a non-existing variable. It’s important to understand why JavaScript might throw errors. If you understand what’s going on and why It is easier and quicker to fix.

Accessing variable before initialization. This Error will Throw only for let and const. For var, it will print undefined

keyvalue
apple'I am apple'
banana<uninitialized>
const apple = 'I am apple'; 
console.log(apple);
console.log(banana);
const banana = "I am banana";

OutPut
// I am apple
// console.log(banana);
//            ^
// ReferenceError: Cannot access 'banana' before initialization

When code is executing line No.3 we are accessing variable banana which is yet not initialized in memory. so we will get an error. To solve the above error, you need to initialize the variable before accessing it.

// Accecing variable which is not defined.
console.log(apple);

output
// console.log(apple);
//            ^
// ReferenceError: apple is not defined
// Accesing variable in wrong scope. variable a have scope inside function.
function inScope() {
   let a = "apple";
 }
console.log(a); 

// ReferenceError a is not defined.

Reference Error is like calling someone by a wrong name.


Type Error

TypeErroroccurs when the variable exists, but the operation you're trying to perform is not appropriate for the type of value it contains.

// apple is variable and we are calling it as function
const apple = 'apple';
apple('πŸ₯');

output
// apple('πŸ₯');
// ^
// TypeError: apple is not a function
// try to push value in string. push is property of array

const apple = 'apple';
apple.push('banane');

// apple.push('banane');
//       ^
// TypeError: apple.push is not a function

Type Error is like asking Fish to climb on a tree.


Β