Type Error vs Reference Error:

Sonika Chaudhary
2 min readFeb 24, 2023

--

In JavaScript, we have three type of error — Type Error, Reference Error and Syntax Error. Here we are discussing about Type Error and Reference Error.

Type Error✍️:

The type error object represents an error when an operation could not be performed because a value is not of the expected type.

Ex-

const num = 45;

console.log(num());

// type error: num is not a function

A Type error may be thrown when:

· An operand or argument passed to a function is incompatible with the type expected by that operator and function.

· When attempting to modify a value that can not be changed.

· When attempting to use a value in an appropriate way.

Summaries it as if a variable type in JavaScript is asked to perform an operation which is not its characteristic, it may occur type error.

Ex- If objects are treated as strings or functions are treated as numbers, it might result in a type error.

let catName = { name: “kitty”,

colour: “white”,

age: 5}

console.log(catName());

// Type Error: catName is not a function

Reference Error:

In JavaScript, if you call a variable which does not exist at that point, it will result in reference error. For example if you call somebody by wrong name who doesn’t exist than that person correct you that you call me by wrong name that is reference error in layman terms.

The reference Error object represent an error when a variable that doesn’t exist in the current scope is referenced.

Ex-

function app() {

console.log(a);

let a = 5; }

app()

// Reference Error: we can not access a before initialized

--

--