Difference between var let and const keyword in javascript

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.

What is SEO

What is SEO

SEO stands for search engine optimization. It is the process of getting traffic from the free, organic, editorial or natural search results on search …

read more
Security Checks in Node Js

Security Checks in Node Js

We can use lots of security check on node js for development and production environment. …

read more