Everything about Reference Error using var keyword.

As a developer, we encounter multiple types of errors in javascript like-

- Reference Error

-Type Error

-Syntax Error

-Internal Error

-Range Error

-Eval Error

Note- There are many types of errors but this blog is limited to Reference errors only.

Getting errors in programming is good as it prevents us from breaking the code. So let's understand the Reference error when it is encountered so that we can fix it.

Reference Error- Reference error occurs when you are trying to refer to or use something which doesn't exit.

Okay, enough with the definition. Let's understand where to look when reference errors occur.

When you encounter the reference error then ask two questions to yourself. Remember this as we are going to ask these questions many times. the questions are-

  1. The variable is defined before accessing the variable?

  2. Are we accessing within the scope only?

As we declare a variable, array, or object through Var, Let, Const. So let's look for var only in this blog.

  • Reference error using Var

    -If you are trying to access the variable, function, array or object before the declaration and definition.

// Variable
var a;
console.log(b); //ReferenceError: b is not defined

// Function
var referenceErrorFucntion=()=>{
return 78
}
console.log(referenceError()); // ReferenceError: referenceErroris not defined

// Array
var arr=[78,89,67];
console.log(array);// ReferenceError: array is not defined

// Object
var obj={Name:"Gautam"}
console.log(objectAccess); //ReferenceError: objectAccess is not defined

Remember two questions that you have to ask yourself to fix the reference error problem-

The variable is defined before accessing the variable? No, then define before accessing. Here we can find the fix with the first question itself so no need to look for the second question.

solution-

//var 
var a;
console.log(a); // undefined 
var b=56;
console.log(b);// 56

//function
var referenceErrorFucntion=()=>{
return 67;
}
console.log(referenceErrorFucntion()); // 67
// Array
var arr=[78,89,67];
console.log(arr); // [78,89,67]

// Object
var obj={Name:"Gautam"}
console.log(obj); // {Name: 'Gautam'}

-If you are trying to access the variable, array or object when out of the scope

As var has function scope which means variable, array or object are defined by the var keyword, it will be accessible throughout the function. As we are limited to reference error only so I will not go deep into the function scope. Please refer to some blogs for this.

//Variable
var variablefunction = () => {
    var age=67;
}
console.log(age); //ReferenceError: age is not defined

//Array
var variablefunction = () => {
    var arr=[98,67];
}
console.log(arr); //ReferenceError: arr is not defined

// Object
var variablefunction = () => {
    var obj={name:"Ashutosh"};
}
console.log(obj) //ReferenceError: obj is not defined

Remember two questions that you have to ask yourself to fix the reference error problem-

The variable is defined before accessing the variable? Yes. then ask yourself the second question.

Are we accessing within the scope only? No. As we know var has the function scope and we are trying to access it outside of the function which is in the global scope. So, it will give the reference error.

Solution-

//Variable
var variablefunction = () => {
    var age=67;
    console.log(age); // 67
}

//Array
var variablefunction = () => {
    var arr=[98,67];
    console.log(arr); // [98,67]
}

// Object
var variablefunction = () => {
    var obj={name:"Ashutosh"};
    console.log(obj);  // Ashutosh   
}

Conclusion- Reference errors occur only when you are trying to access something that is not over there. Mostly it occurs when you are trying to access before the definition or outside of the scope. So make sure these two things are there when you get the reference error and you are good to go.

Happy Learning!!