Difference between ES-5 and ES-6 and (Let,Var and Const)
Table-
Feature | ES5 | ES6 (ES2015) |
Variable Declaration | var is used for variable declaration. | let and const introduced for better variable scoping. |
Block Scoping | Limited block scoping. | Improved block scoping with let and const . |
Default Function Parameters | No support for default function parameters. | Supports default function parameters. |
Arrow Functions | No arrow functions. | Arrow functions introduced for concise function syntax. |
Template Literals | No template literals. | Supports template literals for string interpolation. |
Rest Parameters | No rest parameters. | Rest parameters for gathering function arguments. |
Spread Operator | No spread operator. | Spread operator for expanding elements in arrays or objects. |
Classes | No classes, constructor functions used. | Introduces the class syntax for defining classes. |
Inheritance | Prototypal inheritance pattern. | Improved inheritance with the extends keyword. |
Modules | No native module system. | Introduces ES6 modules for better code organization. |
Promises | No native Promise object. | Promises introduced for better asynchronous programming. |
Generators | No generator functions. | Generator functions with the function* syntax. |
Destructuring | No destructuring. | Destructuring syntax for assignment and extraction. |
Maps and Sets | No native Map and Set objects. | Introduces native Map and Set objects for data structures. |
Symbols | No native Symbol data type. | Introduces the Symbol data type for unique object properties. |
Iterators and Iterables | No built-in support for iterators. | Introduces the concept of iterators and iterables. |
Async/Await | No native async/await. | Introduces async/await for handling asynchronous code in a more readable way. |
Array Methods | Arrays have methods like forEach , map , filter , etc. | Enhanced array methods like find , findIndex , and others. |
String Methods | Limited built-in string methods. | Improved string methods like startsWith , endsWith , and others. |
Object Literal Extensions | No shorthand for defining properties in object literals. | Shorthand property and method definitions. |
let
Feature | var | let | const |
Scoping | Function-scoped. | Block-scoped. | Block-scoped. |
Hoisting | Hoisted to the top. | Hoisted to the top. | Hoisted to the top. |
Reassignment | Allowed. | Allowed. | Not allowed. |
Value Initialization | Not initialized (undefined). | Not initialized (undefined). | Must be initialized when declared. |
Temporal Dead Zone | Not affected by temporal dead zone. | Affected by temporal dead zone. | Affected by temporal dead zone. |
Redeclaration | Allowed. | Not allowed in the same scope. | Not allowed in the same scope. |
Global Object Property | Creates properties on the global object. | Does not create properties on the global object. | Does not create properties on the global object. |
Use Cases | Limited use due to hoisting issues. | Preferred for most cases with block scope. | Preferred for constants and when variable reassignment is not required. |
It's important to note that choosing between var
, let
, and const
depends on the specific use case and the desired behavior. let
and const
are generally preferred over var
in modern JavaScript development because they offer better scoping and reduce common programming errors.
scope
Scope | Description | Example |
Global Scope | Variables declared outside all functions. | javascript let globalVar = 10; |
Accessible from anywhere in the code. | ||
They have a lifetime of the entire | ||
program and persist until it ends. | ||
Function Scope | Variables declared inside a function. | javascript function foo() { let localVar = 5; } |
Only accessible within that function. | ||
Local to the function and not visible | ||
outside. | ||
Block Scope | Variables declared inside a block (e.g., | javascript if (true) { let blockVar = 20; } |
within if , for , while statements). | ||
Limited to the block where they are | ||
declared. | ||
Introduced with ES6 (let and const). | ||
Lexical Scope | Scope determined by the location of the | javascript let x = 10; function outer() { let y = 20; function inner() { console.log(x, y); } inner(); } |
variable's declaration within the code. | ||
Also known as "static scope." |
In the example for lexical scope, the inner
function can access variables from the outer
and global scopes because of the way scopes are nested and determined by the structure of the code.
Global Scope:
javascriptCopy code// Global variable
let globalVar = 10;
function example() {
// Access globalVar in the function
console.log(globalVar);
}
example(); // Outputs: 10
In this example, globalVar
is accessible within the example
function because it's in the global scope.
Function Scope:
javascriptCopy codefunction foo() {
// Local variable
let localVar = 5;
console.log(localVar);
}
foo(); // Outputs: 5
// Attempting to access localVar here will result in an error
// because it's not defined in this scope.
localVar
is a local variable and can only be accessed within the foo
function.
Block Scope (ES6 with let
):
javascriptCopy codeif (true) {
// Block-scoped variable
let blockVar = 20;
console.log(blockVar);
}
// Trying to access blockVar here will result in an error
// because it's not defined in this scope.
blockVar
is scoped to the block defined by the if
statement and cannot be accessed outside of it.
Lexical (Static) Scope:
javascriptCopy codelet x = 10;
function outer() {
let y = 20;
function inner() {
console.log(x, y); // Accesses x from outer scope and y from its own scope
}
inner();
}
outer(); // Outputs: 10 20
In this example, inner
can access x
from the outer scope and y
from its own scope. It uses lexical (static) scoping to resolve variable references.
These examples demonstrate how variables' scope works in JavaScript. Global variables are accessible anywhere, function scope restricts access to the function, block scope (with let
) is confined to specific blocks, and lexical scope follows the nested structure of code.
complete
Thanks