🙏Namaskaram🙏
In javascript "let", "var", and "const" are used to declare variables, but they have some differences in terms of their scope and behavior.
1 const Example1 = () => {
2 var x = 10;
3 if (true) {
4 var x = 20; // This will overwrite the outer variable
5 console.log(x); // Output: 20
6 }
7
8 const myFunction = () => {
9 var x = 30;
10 console.log("function scope", x); // Output: 30
11 };
12 myFunction();
13
14 const myFunction2 = () => {
15 console.log("Point two", x) // Output: 20
16 }
17 myFunction2()
18
19 console.log(x); // Output: 20
20 };
1 const Example2 = () => {
2 let x = 10;
3 if (10 === 10) {
4 let x = 20; // This creates a new variable in a different scope
5 console.log(x); // Output: 20
6
7 x = 30 // Reassign
8 console.log(x) // Output: 30
9
10 // let x = 40 // wrong ❌ not redclarable
11 }
12 console.log(x); // Output: 10
13 };
14
1const Example3 = () => {
2 const x = 10;
3 if (true) {
4 const x = 20; // This creates a new variable in a different scope
5 console.log(x); // Output: 20
6 }
7 console.log(x); // Output: 10
8
9 // x = 30; // cannot reassign
10
11 // const x = 40; // cannot redclare
12 };