1var:
2 - hoisted (always declared at top of scope, global if none)
3 - function scope
4let:
5 - block scope
6 - not redeclarable
7const:
8 - block scope
9 - not reassignable
10 - not redeclarable
11
12Note: Although it may seem like these hold only semantic meaning, using the
13appropriate keywords helps the JS engines' compiler to decide on what to optimize.
1//let is used to declare the variable or initialize a variable..
2//let variable can be updated in future.
3//Its block scope and we cannot redeclare it.
4
5let a; // Initializing the variable.
6let b = 4; // Initializing and declaring variable.
7let b = 5; //Error:- cannot redeclare a value but we can update the value.
8
9//var is just like let but var is used to declare a variable value at top of
10//the program, its hoisted.
11
12var a; // Initializing the variable.
13var b = 4; // Initializing and declaring variable.
14var b = 5; //b will be redeclared and we can update its value.
15
16//const variable should be declared and intialized at same time.
17//const variable cannot be updated in future.
18//we cannot even redeclare the variable in future.
19
20const a = 10; //Initilizing and declaring a variable at a time.
21const b; //Error:- const should be declared and intialized.
22const a = 11 // Error:- cannot redeclare the variable again.
1The difference is that with const you can only only assign a value to a variable
2once, but with let it allows you to reassign after it has been assigned.
1// ⚡ Initialization
2var a; //✅
3let b; //✅
4const c; //❌ const must have an initial value
5
6// ⚡ Re-Assignment
7var d = 1;
8d = 10; //✅
9
10let e = 2;
11e = 20; //✅
12
13const f = 3;
14f = 30 //❌ const value is always constant and cannot be reassigned
15
16// ⚡ Re-Declaration
17var g = 1;
18var g = 10; //✅
19
20let h = 2;
21let h = 20; //❌ must use variable name only to reassign, cannot be reassigned
22
23const i = 3;
24const i = 30 //❌ cannot be reassigned
25
26// ⚡ Scope [Global, Function or local and block scope]
27// Global Scope ==> Any variable that is declared outside any of the function
28// Function Scope(var) ==> Can be accessed inside the function it was declared and its child
29// Block Scope(let and const) ==> Con be accessed inside curly brackets where it was declared and its child
30
31function myFunction(){
32 var myName1 = 'Asmita';
33 let myName2 = 'Dikshit';
34 const myName3 = 'Sizen';
35 console.log(myName1);
36 console.log(myName2);
37 console.log(myName3);
38
39 if(true){
40 console.log(myName1);
41 console.log(myName2);
42 console.log(myName3);
43
44 var myAge1 = 13;
45 let myAge2 = 15;
46 const myAge3 = 14;
47 console.log(myAge1);
48 console.log(myAge2);
49 console.log(myAge3);
50 }
51
52 console.log(myName1);
53 console.log(myName2);
54 console.log(myName3);
55
56 console.log(myAge1);
57 console.log(myAge2);
58 console.log(myAge3);
59};
60myFunction();
1//functional scope
2 var a; // declaration
3 a=10; // initialization;
4//global scope
5// re-initialization possible
6 let a;//only blocked scope & re-initialization possible
7 a=10;
8let a =20;
9if(true){
10 let b =30;
11}
12console.log(b); // b is not defined
13const // const also blocked scope,Re-initialization and re-declaration not possible
14const a; // throws error {when we declaring the value we should assign the value.
15const a =20;
16if(true){
17 const b =30;
18}
19console.log(b); // b is not defined
20console.log(a); // no output here because code execution break at leve b.
21
1var variable1 // declared using var
2const variable2 // declared using const
3
4myFunction1();
5
6function myFunction1() {
7 variable1 = "hello!";
8 console.log(variable1);
9 // "hello!"
10}
11
12myFunction2();
13
14function myFunction2() {
15 variable2 = "hello!";
16 // error: variable2 is a constant and can not be redifined
17}
18
19myFunction3(true);
20
21myFunction3(codition) {
22 if(condition) {
23 let variable3 = "helo!" // declared using const
24 }
25 variable3 = "hello!";
26 // error: variable3 is out of scope
27}
28
29/*
30
31Therefore,
32
33 var:
34 -can be declared outside any function to be used inside any function
35 -can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
36 -can be changed again and again anywhere
37 let:
38 -can be declared outside any function to be used inside any function
39 -if declared inside any function or any other {} which are of only if or if-else or switch etc. and can't be used anywhere inside the function and can be only used inside statement
40 - can be changed again and again only inside the statement in which they are made in
41 const:
42 -can be declared outside any function to be used inside any function
43 -can be declared inside any function or any other {} which are of only if or if-else or switch etc. and can be used anywhere inside the function
44 -cannot be changed again and agan anywhere, if tried to, will result in an error
45
46*/