In Javascript, when you declare variable using var keyword
- The variable declared using var keyword outside the function are global scope.
- The variable declared using var keyword within the function are function scope
- The variable declared using var keyword will get hoisted
console.log(instagramId) // undefined
var instagramId = "devkode.ioπ"; // global scope
function getName(){
console.log(instagramName); // undefined
var instagramName = "devkodeπ" // function scope
console.log(instagramName) // devkodeπ
console.log(instagramId) // devkode.ioπ
}
console.log(instagramId) //devkode.ioπ
console.log(instagramName) // ReferenceError
The variable declared using var keyword can be redeclared and reassign
var foo = "πππ";
console.log(foo); // πππ
var foo = "π€©π€©π€©"
console.log(foo); // π€©π€©π€©
foo = "πππ";
console.log(foo); // πππ
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution.
console.log(name); //undefined
var name="devkode"
The reason for the undefined output is that javascript engine will hoists function declarations and variable declarations to the top of the current scope.
So, the above code will be written as
var name = undefined;
console.log(name); //undefined
var name="devkode";
- The variable declared using let and const keyword are block scope
- The variable declared using let can be reinitialized but can't redeclare.
- The variable declared using const can't be reinitialized and redeclare
{
var instagramName = "devkodeπ";
let instagramId = "devkode.io"
const language = "JavaScript"
}
console.log(instagramName); // devkodeπ
console.log(instagramId); // ReferenceError
console.log(language); // ReferenceError
let bar = "πππ";
console.log(bar); // πππ
bar = "πππ";
console.log(bar); // πππ
let bar = "π€©π€©π€©"
console.log(bar); // SyntaxError, Identifier 'bar' has already been declared
const bar = "πππ";
console.log(bar); // πππ
bar = "πππ";
console.log(bar); // TypeError: Assignment to constant variable
const bar = "π€©π€©π€©"
console.log(bar); // SyntaxError, Identifier 'bar' has already been declared
/********************************* Explained about TDZ (Optional, we can remove) **************************/ Although let variables are hoisted, you will still get a ReferenceError, because until you initialize a let variable, it stays uninitialized and goes into a temporal dead zone, where these variables canβt be accessed.
lets run some sample code:
console.log(name); // ReferenceError
let name="devkode";
Accessing the let and const variable before the initialization results in a ReferenceError. The name variable is in a temporal dead zone (TDZ) from the start of the block until the initialization is processed.
/********************************* Explained about TDZ **************************/
var | let | const | |
---|---|---|---|
Scope | global or function | block | block |
redeclare? | yes | no | no |
reassign? | yes | yes | no |
hoisted? | yes | no | no |