In this article,we have covered different scenario like scope, declaration, shadowing and hoisting to understand var let and const.
Blog
First of all a basic doubt which student have about var let and const is that "are they data types" ?
So the answer is "No". Java script is a dynamically typed language to you dont need to define a datatype explicitly (it can be done if you want to). Var, let and const as an keyword which basically acts a an container to store the value defined.
Now since we are clear wth the baove doubt , to actually understand var, let and const, we need to understand how these keyword behaves under different scenario.
One you know this, you will have an crystal clear understanding on these keywords.
You can also watch my video on the same
{var a = 5;let b = 6;const c = 7;}console.log(a);console.log(b);console.log(c);
Let and const cannot be redeclared under the same scope, while var can.
var a = 1;
let b = 2;
const c = 3;
{
var a = 4;
let b = 5;
const c = 6;
console.log(c);
}
console.log(a);
console.log(b);
console.log(c);
output -
6
4
2
3
Variable shadowing means can the value be overridden or not.,
What is Hoisting ?
During the creation phase, java script engines move all the functions and variable declarations to the top of the code.
In summary, the main differences between var
, let
, and const
are their scope, hoisting behavior, and reassignment rules. var
has function-level scope, is hoisted, and can be reassigned. let
has block-level scope, is not hoisted, and can be reassigned. const
also has block-level scope, is not hoisted, and cannot be reassigned.
To learn more about frontend and interview preparation, feel free to book my trail session
Copyright ©2024 Preplaced.in
Preplaced Education Private Limited
Ibblur Village, Bangalore - 560103
GSTIN- 29AAKCP9555E1ZV