FrontendDeveloper.in

ECMAScript question detail

Variable Scoping

The variable scoping determines the visibility or accessibility of a variable within the certain part of the program or region.

In ES6, both const and let keywords allow developers to declare variables in the block scope.

The let statement declares a block-scoped local variable which can be reassigned. i.e, let declaration creates a mutable variable.

let a = 1;

if (a === 1) {
let a = 2;

console.log(a); //2
}

console.log(a); //1

const variables are similar to let variables but they can't be changed through reassignment. i.e, The const declaration creates a read-only reference to a value.

const x = 1;

if (x === 1) {
const y = 2; // You cannot re-assign the value similar to let variable

console.log(y); //2
}

console.log(x); //1
Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399