JavaScript “let” and “var” related interview questions

“var” and “let” are the two most frequently used keywords in JavaScript. We use them to declare variables in JavaScript. When we declare a variable using the keyword var its scope becomes the current enclosing execution scope.

If the variable is declared inside the global scope then it creates a property on the global window object.

When we a variable using the keyword let then it creates a block scope. If the variable is created in global scope then it does not create a property in the global window object.

Following are some interview questions on “let” and “var” keyword:

  • Does “let” creates a property in global window object when declared in global scope?
  • What will happen if we have let foo = 1; let foo;
  • Does the variable declared with keyword “let” hoisted to top of the block?
  • What is temporal dead zone in case of variable declared with keyword “let”?
  • console.log(foo); let foo = 2; What will happen and why?
  • let foo = 1;
    
    if (true) {
      var foo = 2;
    }
    

    What happens when we execute the above code?

  • What is implicit global variable?