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 To Back Up a WordPress Site to Object Storage

Backing up a WordPress site is essential for protecting your data in case of data loss, hacking, or other unforeseen events. One effective way to back up your WordPress site is by using object storage services such as Amazon S3, Google Cloud Storage, …

read more

How to Optimize WordPress on Ubuntu

Optimizing WordPress on Ubuntu involves several steps to improve performance, security, and user experience. Below are some best practices for optimizing WordPress on Ubuntu: optimization practices, you can improve the speed, performance, and securit …

read more