Everything about Reference Error using "let" and "const" keyword.

I have already written the blog using the var keyword. I will use the same content which is common and will try to explain with a fresh perspective so there is no dependency on that blog. I will add the link to that blog at the end of this blog.

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 let and const in this blog.

  • Reference error using Let

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

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

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

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

// Object
let 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-

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

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

// Object
let 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 let has block scope which means a variable, array or object is defined by the let keyword, it will be accessible within that block only.

//variable
let variablefunction = () => {
    if(true){
    let age=67;
    }   
    console.log(age); 
}
variablefunction()// ReferenceError: age is not defined

//variable
let variablefunction = () => {
    if(true){
    let age=67;
    }   
}
console.log(age);//ReferenceError: age is not defined

//function 
let variablefunction = () => {
    if(true)
    {
            let insideFunction=()=>999;
    }
    console.log(insideFunction()); 
}
variablefunction(); //insideFunction is not defined

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

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

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

//object
let variablefunction = () => {
    if(true){
        let 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 let has the block scope and we are trying to access it outside of the block which is in the function and global scope respectively. So, it will give the reference error.

Solution-

//variable
let variablefunction = () => {
    if(true){
    let age=67;
    console.log(age);
    }   

}
variablefunction() //67

Try other's examples by yourself by accessing within the block only as in the above example. if you want to access the same place where you are getting the reference error so defined the variable, object, function, or array in the global scope.

let and const behave the same when it comes to reference errors. So while using const same scenario will come.

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.

Adding the link here for reference error while using the var keyword-

click here

Happy Learning!!