Mastering the Interview Question: Explain var, let, and const in JavaScript

Table of contents

No heading

No headings in the article.

Did your interview not go as you planned?

You know the answer but you are not able to explain it to the interviewer?

Then, my friend, you are not alone. I and many other devs faced the same problems and that's why I'm here to share some tips on how to confidently explain the answer to an interviewer and increase your chances of landing the job.

We'll start by discussing a common question asked in interviews: Explain var, let and const in JavaScript.

Now, let's start with the most fundamental rule that everyone should follow: listen closely to the question and take a pause to think, gather your thoughts and organize your answer, it's okay to take some seconds to think through.

Reread the question with me, and let's come up with a response together. I am waiting...

Now if you were not able to answer then it's okay we are here to learn. On the other hand, if you already have your answer, congratulations!!

The answer should be clear and concise, let's see what would be the ideal response to this question:

Yes, I'm familiar with var, let, and const. These are all keywords used to declare variables in JavaScript. Var is the oldest way to declare a variable in JavaScript, and it has a function scope. This means that a variable declared with var is accessible throughout the function in which it is declared, regardless of where it is declared within the function. Let and const were introduced in ES6 and have block scope. This means that a variable declared with let or const is only accessible within the block of code in which it is declared. However, the difference between let and const is that let allows the reassignment of the variable, while const does not.

This is the most concise and clear way to explain an answer and the next part is to explain your response by giving an example. If you were not able to understand block and function scope then this example will help you to better understand it:

function fun(){
    var a = 10;
    if(true){
        var a = 1;
        a = 2;
    }
    console.log(a); //2
}

In this code example, the value of the variable "a" can be confusing for someone coming from a different programming language. The variable "a" is declared with the "var" keyword inside the function and is initially assigned the value of 10. However, inside the "if" statement, the variable "a" is declared again with the "var" keyword and assigned a value of 1. The surprising thing is that even though the second declaration of "a" is inside the "if" statement, the value of "a" outside the "if" statement is 2, not 10 as you might expect. This is because the second declaration of "a" using the "var" keyword reassigns the value of the existing variable "a", rather than creating a new variable within the block scope of the "if" statement. So, the final value of "a" printed to the console is 2.

let and const variable was introduced to tackle the disadvantages of var keyword:

function fun(){
    let a = 10;
    if(true){
        let a = 1;
        a = 2;
    }
    console.log(a); //10
}

Here you can see that the previous abnormal behavior is tackled using the let keyword and can be done the same with const, the only difference is that it can not be reassigned and will throw an error.

function fun(){
    let a = 10;
    if(true){
        let a = 1;
        a = 2; //Error
    }
    console.log(a); 
}

You can now confidently explain to the interviewer that using the "let" and "const" keywords in your code is recommended due to their benefits. You can even share a real-life example of a problem you encountered while using the "var" keyword, which led you to switch to using "let" and "const". This approach can help prevent bugs and improve the readability and maintainability of your code.

To make your interview more engageable you can ask: "Does that answer your question or is there anything else I can clarify for you?"

The interviewer can ask you a follow-up question regarding hoisting which is related to this question which I will cover in my next article.

To summarize your response you can give a two-line summary: "In summary, var has function scope, while let and const have block scope. Additionally, let allows reassignment of the variable, while const does not."

I have a trivia question for you to solve and discuss in the comments, you can also suggest an alternative solution to avoid and tackle the abnormal behavior of this code:

var i = 10;
for (var i = 0; i<3; i++){
    console.log(i); //?
}
console.log(i); //?

We did it 🤩.

I hope you found this article helpful and that it has given you the confidence to respond effectively to questions during an interview. Thank you for reading.

Until then Happy coding. Stay tuned😉 for the next article.