What is blocked scoped variables ES6 ?
Last Updated :
20 Feb, 2023
In ES5 when we declare a variable with the var keyword, the scope of the variable is either global or local.
But, ECMAScript 2015 (ES6) introduced two new keywords let and const and these two keywords provide Block Scope in JavaScript. All variables that have been declared within curly brackets { } are restricted to be accessed within that block and inside blocks of that block only. We can't access variables outside that block, it'll either through a ReferenceError (variable is not defined) or in case he has a variable with the same name at Global scope then it'll refer to that.
let keyword: Variables declared with var keyword are allowed to redeclare, but Variables declared with let keyword in the same block are not allowed to redeclare. It'll through a SyntaxError: Identifier has already been declared.
Example 1: In this example, we will use let keyword and redefine the variable
JavaScript
function valueAssign() {
let x = 10;
var z = 15;
if (true) {
let y = 5;
console.log(y); // Here y is 5
console.log(x); // Here x is 10
console.log(z); // Here z is 15
}
console.log(x); // Here x is 10
console.log(z); // Here z is 15
console.log(y); // ReferenceError: y is not defined
}
valueAssign();
Output:
5
10
15
10
15
ReferenceError: y is not defined
Here we see x and z both are declared in the outer block with reference of if's block {} and y declare in the inner block. Here z is declared using var keyword so we use z variable everywhere in this valueAssign() function because variables declared with the var keyword can not have block scope. But x and y, both are declared using the let keyword. Here x is declared in the outer block, so we can use x in the inner block. Since y is declared in the inner block, so we can not use it in the outer block. Let and const will work on the block that I will declare and will work on the blocks inside it. For this reason, when we print y in the outer block it gives an error.
Example 2: In this example, we'll see the working of redeclaring variables with var and let:
JavaScript
// Redeclared using var
function valueAssignWithVar() {
var y = 20;
y = 30; // Redeclare allowed
console.log(y);
}
// Redeclared using let in different block
function valueAssignWithLet1() {
let x = 20;
if (true) {
let x = 5; // Redeclare allowed
console.log(x);
}
console.log(x);
}
// Redeclared using let in same block
function valueAssignWithLet2() {
let x = 10;
let x = 5; // Redeclare not allowed
// SyntaxError: Identifier 'x' has
// already been declared
console.log(x);
}
valueAssignWithVar();
valueAssignWithLet1();
valueAssignWithLet2();
Output:
30
5
20
SyntaxError: Identifier 'x' has already been declared
const keyword: Variables declared with const keyword are strictly not allowed to redeclare and reassign with the same block. We use the const keyword when you do not want to change the value of that variable in the whole program.
Example 1: In this example, we'll see the working of reassigning variables with const
JavaScript
// Reassigned not allowed
const x = 10;
x = 5; // TypeError: Assignment to constant variable
console.log(x);
Output :
TypeError: Assignment to constant variable.
Example 2: In this example, we'll see the working of redeclaring variables with const
JavaScript
// Redeclared using const in different block
function valueAssignWithConst1() {
const x = 20;
if (true) {
const x = 5; // Redeclare allowed
console.log(x);
}
console.log(x);
}
// Redeclared using const in same block
function valueAssignWithConst2() {
const x = 10;
const x = 5; // Redeclare not allowed
// SyntaxError: Identifier 'x' has
// already been declared
console.log(x);
}
valueAssignWithConst1();
valueAssignWithConst2();
Output:
SyntaxError: Identifier 'x' has already been declared
Example 3: In this example, we'll see the working of Block Scope with const
JavaScript
function valueAssign() {
const x = 10;
var z = 15;
if (true) {
const y = 5;
console.log(y); // Here y is 5
console.log(x); // Here x is 10
console.log(z); // Here z is 15
}
console.log(x); // Here x is 10
console.log(z); // Here z is 15
console.log(y); // Error: y is not defined
}
valueAssign();
Output :
5
10
15
10
15
ReferenceError: y is not defined
Similar Reads
What are Block Scoped variables and functions in ES6 ?
Block-scoped variables and functions are defined inside a particular block or inside curly { } braces and they can only be accessible inside that particular block or within that block. The block-scoped variables were first introduced in EcmaScript2015 or es6. ES6 introduces us with two keywords: let
3 min read
What is Variable Scope in JavaScript ?
Variable scope is the context of the program in which it can be accessed. In programming, a variable is a named storage location that holds data or a value. Think of it as a container that you can use to store and manipulate information in your code. Variables allow you to work with data in a flexib
4 min read
What are the different scopes of variables in PHP ?
Variable Scopes: The scope of a variable is defined as its extent in the program within which it can be accessed, i.e. the scope of a variable is the portion of the program within which it is visible or can be accessed. Depending on the scopes, PHP has three variable scopes. Local variables: The var
3 min read
Scope of a variable
Scope of a variable defines the part of code where the variable can be accessed or modified. It helps in organizing code by limiting where variables are available, preventing unintended changes or conflicts. Understanding scope is key to writing clear, efficient, and maintainable programs. In this a
8 min read
Why to avoid global variables in JavaScript ?
Global variables are variables that are declared or defined outside any functions in the script. This indicates that global variables can be accessed from anywhere in the particular script and are not restricted to functions or blocks. JavaScript global variables can also be declared from within a f
4 min read
What are undeclared and undefined variables in JavaScript?
Undefined: It occurs when a variable has been declared but has not been assigned any value. Undefined is not a keyword. Undeclared: It occurs when we try to access any variable that is not initialized or declared earlier using the var or const keyword. If we use 'typeof' operator to get the value of
1 min read
What is the Temporal Dead Zone in ES6 ?
Introduction: Before going to start, just think about what these 3 words are saying? Temporal means something temporary, not permanent, and Dead means something which is not working or can say it is in a lifeless state, Zone denotes an area but here we are in programming so this term area will be so
6 min read
Variables and Datatypes in JavaScript
Variables and data types are foundational concepts in programming, serving as the building blocks for storing and manipulating information within a program. In JavaScript, getting a good grasp of these concepts is important for writing code that works well and is easy to understand.VariablesA variab
3 min read
What characters are valid for JavaScript variable names ?
In this article, we will see the valid variable names in JavaScript. The valid variable names can contain letters (both uppercase and lowercase), numbers, underscores, and dollar signs. However, the variable names can not begin with numbers. Some examples of valid variable names: var Geeks; var Geek
2 min read
Why use Question Mark in TypeScript Variable?
The question mark in TypeScript is used to mark a variable as optional, allowing it to be omitted or set to undefined. This is commonly used in object properties and function parameters, enhancing flexibility by making variables non-mandatory. This feature helps prevent errors and ensures better han
2 min read