Understanding Let , var , const in Javascript
Day 1 of 30 Days of Javascript Interview Challenge
Welcome to day 1 of the Interview Challenge in Javascript.
The interviewer might ask questions like :
A) What are variables in javascript?
B) Explain the difference btw let, var, and const.
C) Why var is not recommended in Javascript ES6 ?
D) What are block and function scope ?
E) Explain hoisting of variable and difference in var and let on the basis of hoisting ?
Variable in Javascipt :
Variable in Javascript are containers to store the data values with in our programs .
In Javascript , there are three types of variable declaration . Let ,var and const . LET used for block -scoped variables , meaning they're only accessible within the block ( under the curly braces {}) , Const is similar to let in scope but the value cannot be reassinged once it is intialized . While Var used for global scope means it can be redeclared in the same scope with same name .
Difference btw Let , var and const :
LET: Let variable used for block scope variable , they are not hoisted to the top of their scope it can be reassigned the value after intialization.
let name = 'alok tamrarkar ';
let surname = name;
let name = 'ankita';
console.log(name) // ans = ankita
console.log(surname); // ans = alok tamrakar
function name(){
let name = 'alok';
console.log(name); // alok
}
name();
Const : Const is similar to let on scope . they are not hoisted to the top of their scope it cannot be reassinged the value once it is intialized.
const name = 'alok tamrkar ';
name ='anmol'; // not possible
console.log(name) // ans = alok tamrkar
Var : Var used for global scope variable . they are hoisted to the top of their scope , it can be redeclared in the same scope with the same name .
var name = 'aok ';
console.log(name); // aok
function naming(){
var name = 'ankita';
console.log(name);// ankita
}
naming();
let surname = 'alok';
function surnaming (){
console.log(surname); // undiefined because let uses block scope
}
surnaming();
Var is not recommened in ES6 :
Var variable are not recommened due to its hoisting behaviour and global scope declaration . 'var' variable are hoisted to the top of the scope , which can lead to unexpected behaivour also it can be redeclared with the same scope .
Block and Function scope in Javascript:
Block and function scope are both concepts related to variable visiblity and accessbility in javascript.
Function Scope:
JavaScript has function scope: Each function creates a new scope.Variables defined inside a function are not accessible (visible) from outside the function.Variables declared with var
, let
and const
are quite similar when declared inside a function.They all have Function Scope:
Block Scope:
Block scope refers to the visiblity of variables declared within a block of code , such as within loops, if statements , or any code block enclosed within curly braces {}. IN js let and const have block scope. meaning they are accessible with in the block in which they are defined.
Hoisting of Variable :
In Javascript , hosting refers to the process where variable and function declaration are moved to the top of their containing scope during the compilation phase , regardless of where they are declared within that scope . This means that if you even declare a function later in your code. it behaves as if it were declared at the top.
It is important to note that only the declaration are hoisted not the intializations . For variables declared with var , the intialization is also hoisted . but for variable declared with let or const , only the declaration is hoisted , and the intialization remains in its original place.
console.log(name); // not working
let name = 'alok tamrakar';
myFunction();
function myFunction(){
console.log(" hello , world "):
}
console.log(name); // alok this is me
var name = 'alok this is me ';