Difference between var let and const keyword in javascript

var Keyword In JavaScript

We used var keyword to create the local variable. Any variable created by var keyword has only function scope that means we can access this variable inside the same function or inner nested functions only.

As long as the variable is declared inside the function, it’s available in the entire function.

 function myNewFunction(){ 
var myNewVariable1 = 10;  
console.log(myNewVariable1); // 10  

function myNewNestedFunction(){ 
var myNewVariable2 = 15;  
console.log(myNewVariable1); // 10 
console.log(myNewVariable2); // 15  
}  

myNewNestedFunction();  
console.log(myNewVariable2); // ReferenceError: myNewVariable2 is not defined 
} 

myNewFunction();  
console.log(myNewVariable1); // ReferenceError: myNewVariable1 is not defined 
console.log(myNewVariable2); // ReferenceError: myNewVariable2 is not defined 

let Keyword In JavaScript

ES6 (ES2015) introduced the new keywords let and const in javascript to be used side by side with var keyword. The main difference between var and let is that let is block scoped, instead of function scoped.

 function myNewFunction(){ 
let myNewVariable1 = 10;  
console.log(myNewVariable1); // 10  

function myNewNestedFunction(){ 
let myNewVariable2 = 15;  
console.log(myNewVariable1); // 10 
console.log(myNewVariable2); // 15  
}  

myNewNestedFunction();  
console.log(myNewVariable2); // ReferenceError: myNewVariable2 is not defined 
} 

myNewFunction();  
console.log(myNewVariable1); // ReferenceError: myNewVariable1 is not defined 
console.log(myNewVariable2); // ReferenceError: myNewVariable2 is not defined 

const Keyword In JavaScript

All the difference between var and let are also true for var and const keyword in javascript. In other words let and const are almost same. They are both blocked scoped and work with same way. The only thing that makes const different is that is a constant.

A variable created with const cannot be changed after it is initialized but an object in a const variable is not immutable. We can modify the objects created with const keyword.

 const x; // SyntaxError: Missing initializer in const declaration 
const y = 10; 
y = 15; // TypeError: Assignment to constant variable.  

const user = {     
name: "xyz",     
age: 27 
};  

user.age = 35; // This is OK, obj is not immutable 
console.log(user.age); // 35  

So mostly we always try to use const keyword for create the variable and if we want to reassigned the variable then we need to use let keyword. In ES6 we try to avoid the use of var keyword.

How is host application deployment different from container application dep …

Host application deployment and container application deployment differ significantly in their approaches and characteristics: container application deployment offers several advantages over traditional host-based deployment, including improved isola …

read more

How To Handle CPU-Bound Tasks with Web Workers

Handling CPU-bound tasks with Web Workers in JavaScript allows you to offload heavy computations from the main thread, preventing it from becoming unresponsive. Here's a step-by-step guide on how to do this: Handling CPU-bound tasks with Web Workers …

read more