Reference Error vs TypeError

Reference Error

When a variable is called before it is declared. It throws a 'reference error' in that scope.

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

This will throw a reference error. As 'a' is not yet been declared yet.

For a error not to be thrown, not only the variable need to be declared but declared in the scope it is being called.

function someFunction(){
    let a = "aman"
    console.log("1", a)
}
console.log("2", a)

The "1" console will result in "aman".

"2" will result in reference error.

As "a" is defined as "let" which is a block scope i.e. "a" is only accessible inside the "someFunction" scope not outside of it.

"2" console has no access to "a" inside the function, so it will throw reference error.

Type Error

When a certain value is expected by a function but it gets the value of unexpected type due to which the operation could not be performed, then it throws TypeError.

let myAge = 11
console.log(myAge.toUpperCase())

As "11" is a type = number and "toUppercase()" is a inbuilt function applied only to type "string", the function is expecting a "string" but gets unexpectedly a number so it throws a typeError.

That 11 is not the type it is expecting.

Thanks for reading.