Errors in JavaScript, dedicated to ReferrenceError and Syntax Error.

Errors in JavaScript, dedicated to ReferrenceError and Syntax Error.

ยท

5 min read

Overview

As a programmer, I personally love JavaScript programming language to an extent, as it usage has a wide impact in the globe. Before, I started learning to write code, I did not like much and I explored other programming languages like C, C++, .Net, Java and Python. I learned and enjoyed because of its complexity. Even for printing a single line of code I had to write at least 5-10 lines of code in it, but case was not much with Python. Eventually when I dedicated myself to dive deep into web Development I had to start learning JavaScript and solve daily small problems. Which is still continue doing so. Although I faced lot of different errors in those language was not an issue much.

Here in this article I would like to put forward all the different types of an error with more focus on the referrence error and type error. These errors can make a program misbehave , causing frustration for the developers.

The different types of errors are :-

  1. Syntax Error

  2. Type Error

  3. Reference Error

Syntax Error

It is one of the most common error in JavaScript programming language. This error occurs when there is a possible error in it's grammar like rules. Therefore SyntxError Object represents error when we interpret in wrongly. JavaScript has a specific set of rules that enforces how the codes should be written in JavaScript.

Possibility of SyntaxError can occurs are :-

  • when the semicolon is missing , in the end of the statement which is very rarely occured.

  • When the operator is mistakenly used, as the mathematical operation is wrongly performed.

  • when the keyword are misspelled, like names of the variables or the functtion names can cause the syntax error.

Let's us see the example below what happens when we forget to close the parenthesis in console.log();

let x = 5;
console.log(x;
// Expected Error : Uncaught SyntaxError: missing ) after argument list

Other example here is about the wrong operator implementation.

let b = 5;
let c = 6;
let a = b\c;
console.log(a);
//Expected Error: Uncaught SyntaxError: Invalid or unexpected token

Type Error

Even Type Error is a common error in JavaScript. TypeError can occur when trying to perform an operation on a variable or object and it does not support the operations. Type error can be cause by different factor and also they can be avoided when consious about writing code. We shall understand how to prevent the type error from occuring.

A Type Error can be thrown when ever the operand and the argument passed to the function is not compatible with the expected type by the operator, then an error thrown is known as Type Error.

Possibility of Type Error can occurs are :-

  • Encountering unexpected

  • Inappropriate use of the operators

  • when the variable is missing or is undefined.

You can possibly encounter Uncaught TypeError when are trying to convert the Number into an uppercase character or vice-versa

const a = 5;
try {
a.toLowerCase();
}
catch(error){
    console.log(error.name);
}

// Uncaught TypeError : can appear.

TypeError can pop up even when the Incorrect function argument is passed, where its expectation is to get String but receives Number.

function myNum(a,b){
return a + b;
}

const sendVal = myNum(5, "2");
// Even here, TypeError : cannot add a number and a string can appear.

One more important ways to get Type error can also be trying to use the variable that has not been allocated in the memory. It can only happen when is declared and has not been initialised.

const data;
console.log(data.name);
// Type Error : because data is undefined.

Reference Error

This error can is also one of the commonly occuring error in JavaScript , for the beginners if they are not familiar with this error then they will not be able to tackle the error in one go. Therefore it is very essential to know about the reference error. This error can occur when the variable that doesn't exist is the scope is reference.

There are different types of references and they are as follows :-

  • Undefined variables

  • Scope or it out of scope

Variables must be declared before they are used. If the variable is absent then the interpreter will throw a Reference Error

console.log(name);
// ReferenceError : name is not defined

The above error can be avoided with a technique by declaration of the variable . So, that it does not throw us a reference error instead we can replace with the undefined

const name;
console.log(name);
// undefined

Variables defined inside a function's scope cannot be accessed outside of it. we can think of it as protocols that needs to be followed.

function myNam(){
const firstName = "Hashnode";
const lastName = "Blog";
return firstName + lastName;
}

console.log(firstName);
// Uncaught ReferenceError : firstName is not defined.

The above codes error can also be fixed by declaring the variables in global scope.

const firstName = "Hashnode";
const lastName = "Blog";

function myNam(){
return firstName + lastName;
}

console.log(firstName);
// Hashnode

I hope you enjoyed reading it and got to learn a lot from this content. Make sure to drop a comment for further improvement of the content, if you feel that some more points can be added to it. I would be happy to get your suggestion on this.

Happy Coding ๐Ÿ’–

Have a Great Day!

ย