Var , Let, Const , Hoisting and Scope in JavaScript

Firstly Quick Introduction about javaScript.

First appeared javascript in December 4, 1995 Designed by Brendan Eich in Netscape communication corporation.

Javascript a multi-paradigm language paradigm means which paradigms are a way to classify programming languages based on their features.

if we are taking about JavaScript . JavaScript supports event-driven, functional, and imperative (including object-oriented and prototype-based) and DOM programming styles .

So JavaScript latest stable release 9th Edition ECMAScript 2018 in June.

But in ES6 introduce Let and Const in 2015. How it work?

<script>
for (var i = 0; i < 5; i++) { 
 document.write("using var:" + i); //inside forloop block scope using var
}
document.write("After Scope var :"+i); //outside forloop block scope
for (let x= 0; x < 5; x++) {
 document.write("using let:"+ x); //inside forloop block scope 
}
document.write("After Scope let:" + x); //outside forloop block scope
 
</script>
Output will be
using var: 01234
After Scope var : 5
using let: 01234
After Scope let: x is undefined

In JavaScript there are three types of scope: above we saw

Block scope , Global scope, Local scope

Scope determines the accessibility and visibility of variables.

<script> 
var x = 10;
let y =22; 
var z =1155;
let r=1122;

 const x = 2;
 const y = 12;
 document.write(“Block Scope const var x is “ + x + “<br/>”); 
 document.write(“Block Scope const let y is “ + y + “<br/>”); 
 //x=46; y=98; //after using const we can not change var/let value it will showing error
document.write(“Block Scope const var z is “ + z + “<br/>”); document.write(“Block Scope const var r is “ + r + “<br/>”);
}
 document.write(“Global Scope var x is “ + x + “<br/>”); 
 document.write(“Global Scope let y is “ + y + “<br/>”); 
 y =33;
 document.write(“Global Scope Change the value y “ + y + “<br/>”);
 // var y=33; let y=33; //it is showing error Identifier ‘y’ has already been declared
 var x = 130;
 document.write(“Global Scope Change var x is “ + x + “<br/>”); 
</script>

output will be 
Block Scope const var x is 2
Block Scope const let y is 12
Global Scope var x is 10
Global Scope let y is 22
Global Scope Change the value y 33
Global Scope change var x is 130

<p id=”demo”></p>

<script>
myFunction(); // after call you can use inside variable in the code like carName and X
// code here can use carName as a global variable
document.getElementById(“demo”).innerHTML = “I can display “ + carName;
document.write(“value of x:”+ x); 
myFunction();

function myFunction() {
 varcarName = “Volvo”;
 x=93;
 document.write(“in func x: ”+x);
}
</script>

Output : I can display Volvo

in func x: 93 
value of x:93 
in func x: 93

let allows you to declare variables that are limited in scope to the block

<script> 
var x = 10; //As var x define global Scope
let y =22; //As let y define global Scope
var z =1155; //As var z define global Scope
let r=1122; //As let r define global Scope

 const x = 2;
 const y = 12;
 document.write(“Block Scope const var x is “ + x + “<br/>”); 
 document.write(“Block Scope const let y is “ + y + “<br/>”); 
 //x=46; y=98; //after using const we can not change var/let value it will showing error
 document.write(“Block Scope const var z is “ + z + “<br/>”);
 document.write(“Block Scope const let r is “ + r + “<br/>”);
 let q =778; // As let q define block Scope
}
 document.write(“Global Scope var x is “ + x + “<br/>”); 
 document.write(“Global Scope let y is “ + y + “<br/>”); 
 y =33;
 document.write(“Global Scope Change the value y “ + y + “<br/>”);
 // var y=33; let y=33; //it is showing error Identifier ‘y’ has already been declared
 var x = 130;
 document.write(“Global Scope Change var x is “ + x + “<br/>”);
//document.write(“Global Scope let q is “ + q + “<br/>”); 
 
 let q=334;
 document.write(“Global Scope let q is “ + q + “<br/>”); 
 
</script>
Block Scope const var x is 2
Block Scope const let y is 12
Block Scope const var z is 1155
Block Scope const let r is 1122
Global Scope var x is 10
Global Scope let y is 22
Global Scope Change the value y 33
Global Scope Change var x is 130
Global Scope let q is 334

We can not use block scope let and Const out of function . we can only use global scop mean var

<script>
myFunction(); 
document.write(“value of x:”+ x); 
 //document.write(“value of se:”+ se); // se is not defined
//document.write(“value of cname:”+ cname ); //cname is not defined
myFunction();
function myFunction() {
 carName = “Volvo”;
 x=93;
 document.write(“in func x: “+x);

let cname=”ggg”; //it can not use out of function 
 const se=113;
}
</script>

<script>
logIt(); // output will be : undefined
var text = 'outside';
function logIt(){
document.write(text);
// var text = 'inside';
};
logIt(); // output will be : outside
</script>
Like what you read? Give Avinash Maurya a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.