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 Open a Port on Linux

Opening a port on Linux involves configuring the firewall to allow traffic through the specified port. Here's a step-by-step guide to achieve this, assuming you are using ufw (Uncomplicated Firewall) or iptables for managing your firewall settings. u …

read more

Troubleshooting Latency Issues on App Platform

Troubleshooting latency issues on an app platform can be complex, involving multiple potential causes across the network, server, application code, and database. Here’s a structured approach to identifying and resolving latency issues. Identify …

read more