Documentation
javascript
var, let, const

Interview Questions related to var, let and const

What is the difference between function scoped and block scoped?

  • Function scoping and block scoping are two ways that variables can be scoped in JavaScript.

  • In function scoping, a variable declared with var inside a function is accessible within the function, as well as within any nested functions. These variables are not accessible outside the function in which they are declared.

  • In block scoping, a variable declared with let or const is only accessible within the block in which it is declared and any nested blocks. The variable is not accessible outside the block.

Example of function scoping:

function exampleFunction() {
  var functionScoped = "I am function scoped";
  if (true) {
    console.log(functionScoped); // "I am function scoped"
  }
}
console.log(functionScoped); // ReferenceError: functionScoped is not defined

Example of block scoping:

if (true) {
  let blockScoped = "I am block scoped";
}
console.log(blockScoped); // ReferenceError: blockScoped is not defined

What is the difference between var, let and const?

  • var is a function-scoped variable, while let and const are block-scoped variables.
  • var variables can be re-declared and re-assigned within the same scope, while let variables can be re-assigned but not re-declared, and const variables cannot be re-assigned or re-declared.

What is the difference between var and let?

  • The main difference between var and let is that var is function-scoped and can be re-declared and re-assigned within the same scope, while let is block-scoped and can only be re-assigned but not re-declared.

What is the difference between let and const?

  • The main difference between let and const is that let variables can be re-assigned within the same scope, while const variables cannot be re-assigned or re-declared.

What happens when you use var in a block-scoped context, such as inside an if statement or a for loop?

  • When var is used in a block-scoped context, it is still function-scoped and can be accessed outside of the block in which it was declared.

Example:

if (true) {
  var blockScopedVar = "I am block-scoped with var";
}
console.log(blockScopedVar); // "I am block-scoped with var"

What happens when you use const in a block-scoped context, such as inside an if statement or a for loop?

  • When const is used in a block-scoped context, it is block-scoped and cannot be accessed outside of the block in which it was declared, and cannot be re-assigned or re-declared.

Example:

if (true) {
  const blockScopedConst = "I am block-scoped with const";
}
console.log(blockScopedConst); // ReferenceError: blockScopedConst is not defined

What happens when you try to re-declare a var variable within the same scope?

  • When you try to re-declare a var variable within the same scope, it will not throw an error and the value of the variable will be updated.

Example:

var reDeclaredVar = "I am the original value";
var reDeclaredVar = "I am the updated value";
console.log(reDeclaredVar); // "I am the updated value"

What happens when you try to re-declare a let variable within the same scope?

  • When you try to re-declare a let variable within the same scope, it will throw a SyntaxError and the value of the variable will not be updated.

Example:

let reDeclaredLet = "I am the original value";
let reDeclaredLet = "I am the updated value";
console.log(reDeclaredLet); // SyntaxError: Identifier 'reDeclaredLet' has already been declared

What happens when you try to re-declare a const variable within the same scope?

  • When you try to re-declare a const variable within the same scope, it will throw a SyntaxError and the value of the variable will not be updated.

Example:

const reDeclaredConst = "I am the original value";
const reDeclaredConst = "I am the updated value";
console.log(reDeclaredConst); // SyntaxError: Identifier 'reDeclaredConst' has already been declared

What happens when you try to re-assign a var variable within the same scope?

  • When you try to re-assign a var variable within the same scope, it will not throw an error and the value of the variable will be updated.

Example:

var reAssignedVar = "I am the original value";
reAssignedVar = "I am the updated value";
console.log(reAssignedVar); // "I am the updated value"

What happens when you try to re-assign a let variable within the same scope?

  • When you try to re-assign a let variable within the same scope, it will not throw an error and the value of the variable will be updated.

Example:

let reAssignedLet = "I am the original value";
reAssignedLet = "I am the updated value";
console.log(reAssignedLet); // "I am the updated value"

What happens when you try to re-assign a const variable within the same scope?

  • When you try to re-assign a const variable within the same scope, it will throw a TypeError and the value of the variable will not be updated.

Example:

const reAssignedConst = "I am the original value";
reAssignedConst = "I am the updated value";
console.log(reAssignedConst); // TypeError: Assignment to constant variable.

What is the difference between var, let, and const when it comes to variable scope?

  • var variables have function scope, which means that they are accessible within the entire function in which they are declared, including inner blocks.
  • let and const variables have block scope, which means that they are only accessible within the block in which they are declared, and not accessible in inner blocks.

Example:

function varScope() {
  if (true) {
    var varInIf = "I am accessible in if";
  }
  console.log(varInIf); // "I am accessible in if"
}
 
function letScope() {
  if (true) {
    let letInIf = "I am not accessible outside if";
  }
  console.log(letInIf); // ReferenceError: letInIf is not defined
}
 
function constScope() {
  if (true) {
    const constInIf = "I am not accessible outside if";
  }
  console.log(constInIf); // ReferenceError: constInIf is not defined
}

Can you change the value of a const variable after it has been declared?

  • No, you cannot change the value of a const variable after it has been declared. Attempting to change the value of a const variable will result in a TypeError.

Example:

const constantVar = "I am constant";
constantVar = "I am trying to change";
console.log(constantVar); // TypeError: Assignment to constant variable.

What happens when you declare a const variable without initializing it?

  • When you declare a const variable without initializing it, it will result in a SyntaxError.

Example:

const uninitializedConst;
console.log(uninitializedConst); // SyntaxError: Missing initializer in const declaration

What happens when you declare a var variable without initializing it?

  • When you declare a var variable without initializing it, it is assigned a value of undefined.

Example:

var uninitializedVar;
console.log(uninitializedVar); // undefined

What is the difference between declaring a variable with var and declaring a variable with let or const inside a for loop?

  • When you declare a variable with var inside a for loop, it is function scoped and is accessible outside the loop.
  • When you declare a variable with let or const inside a for loop, it is block scoped and is not accessible outside the loop.

Example:

for (var i = 0; i < 3; i++) {
  // do something
}
console.log(i); // 3
 
for (let i = 0; i < 3; i++) {
  // do something
}
console.log(i); // ReferenceError: i is not defined
 
for (const i = 0; i < 3; i++) {
  // do something
}
console.log(i); // ReferenceError: i is not defined

What happens when you declare a let variable with the same name as an existing variable in the same scope?

  • When you declare a let variable with the same name as an existing variable in the same scope, it will result in a SyntaxError.

Example:

let existingVar = "I am existing";
let existingVar = "I am trying to re-declare";
console.log(existingVar); // SyntaxError: Identifier 'existingVar' has already been declared

What happens when you declare a const variable with the same name as an existing variable in the same scope?

  • When you declare a const variable with the same name as an existing variable in the same scope, it will result in a SyntaxError.

Example:

const existingConst = "I am existing";
const existingConst = "I am trying to re-declare";
console.log(existingConst); // SyntaxError: Identifier 'existingConst' has already been declared

Can you declare multiple variables with the same name in different scopes with let or const?

  • No, you cannot declare multiple variables with the same name in different scopes with let or const. Attempting to do so will result in a SyntaxError.

Example:

function differentScope() {
  let sameVar = "I am in function scope";
  if (true) {
    let sameVar = "I am in if scope";
    console.log(sameVar); // "I am in if scope"
  }
  console.log(sameVar); // "I am in function scope"
}