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
varinside 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
letorconstis 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 definedExample of block scoping:
if (true) {
let blockScoped = "I am block scoped";
}
console.log(blockScoped); // ReferenceError: blockScoped is not definedWhat is the difference between var, let and const?
varis a function-scoped variable, whileletandconstare block-scoped variables.varvariables can be re-declared and re-assigned within the same scope, whileletvariables can be re-assigned but not re-declared, andconstvariables cannot be re-assigned or re-declared.
What is the difference between var and let?
- The main difference between
varandletis thatvaris function-scoped and can be re-declared and re-assigned within the same scope, whileletis block-scoped and can only be re-assigned but not re-declared.
What is the difference between let and const?
- The main difference between
letandconstis thatletvariables can be re-assigned within the same scope, whileconstvariables 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
varis 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
constis 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 definedWhat happens when you try to re-declare a var variable within the same scope?
- When you try to re-declare a
varvariable 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
letvariable within the same scope, it will throw aSyntaxErrorand 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 declaredWhat happens when you try to re-declare a const variable within the same scope?
- When you try to re-declare a
constvariable within the same scope, it will throw aSyntaxErrorand 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 declaredWhat happens when you try to re-assign a var variable within the same scope?
- When you try to re-assign a
varvariable 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
letvariable 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
constvariable within the same scope, it will throw aTypeErrorand 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?
varvariables have function scope, which means that they are accessible within the entire function in which they are declared, including inner blocks.letandconstvariables 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
constvariable after it has been declared. Attempting to change the value of aconstvariable will result in aTypeError.
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
constvariable without initializing it, it will result in aSyntaxError.
Example:
const uninitializedConst;
console.log(uninitializedConst); // SyntaxError: Missing initializer in const declarationWhat happens when you declare a var variable without initializing it?
- When you declare a
varvariable without initializing it, it is assigned a value ofundefined.
Example:
var uninitializedVar;
console.log(uninitializedVar); // undefinedWhat 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
varinside a for loop, it is function scoped and is accessible outside the loop. - When you declare a variable with
letorconstinside 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 definedWhat happens when you declare a let variable with the same name as an existing variable in the same scope?
- When you declare a
letvariable with the same name as an existing variable in the same scope, it will result in aSyntaxError.
Example:
let existingVar = "I am existing";
let existingVar = "I am trying to re-declare";
console.log(existingVar); // SyntaxError: Identifier 'existingVar' has already been declaredWhat happens when you declare a const variable with the same name as an existing variable in the same scope?
- When you declare a
constvariable with the same name as an existing variable in the same scope, it will result in aSyntaxError.
Example:
const existingConst = "I am existing";
const existingConst = "I am trying to re-declare";
console.log(existingConst); // SyntaxError: Identifier 'existingConst' has already been declaredCan 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
letorconst. Attempting to do so will result in aSyntaxError.
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"
}