Shadowing: ========== ==> it is a process of re-declaring same name identifier within the scope. ==> there are three different scopes for variables: 1) Block Scope/Local Scope 2) Function Scope 3) Global Scope 4) Module Scope ==> Within OOP 1) Block Scope: ================ ==> The variable can able to access within the same block (in which block, we have defined) "use strict"; var x; x = 10; const p = 123; if(x == 10){ const y = 20; // y = 20; console.log("x = "+x+"y = "+y); console.log("p = "+p); } // console.log("x = "+x+"y = "+y); 2) Function Scope ================= ==> The variable which we have defined within the function, and that can allowed to access within the same function. That variable is called as "Function scope variable". "use strict"; function f1() { var x; let y; const z = 123; x = 100; y = 200; console.log("x = "+x); console.log("y = "+y); console.log("z = "+z); } function f2() { // console.log("x = "+x); // console.log("y = "+y); // console.log("z = "+z); console.log("Hi"); } f1(); f2(); ====================================== Global Scope: ============= The variables we can able to access in all the functions of the same programs, those variables are called as "Global Variables". "use strict"; var x = 100; let y = 200; const z = 300; function f1() { console.log("x = "+x); console.log("y = "+y); console.log("z = "+z); } function f2() { console.log("x = "+x); console.log("y = "+y); console.log("z = "+z); } f1(); f2(); Q: Do we able to change the global variable? ============================================= yes. "use strict"; var x = 100; let y = 200; const z = 300; function f1() { console.log("x = "+x); console.log("y = "+y); console.log("z = "+z); } function f2() { var x = 101; y = 202; // z = 303; console.log("x = "+x); console.log("y = "+y); console.log("z = "+z); } function f3() { console.log("x = "+x); console.log("y = "+y); console.log("z = "+z); } f1(); f2(); f3(); f1(); =============================== Note: ==== Shadowing is applicable/allowed for only var but not to let and const. "use strict"; var x = 10; let y = 20; const z = 30; // re-assignment/assignment is not possible console.log(x); console.log(y); console.log(z); // Assignments x = 20; y = 30; // z = 40; console.log(x); console.log(y); console.log(z); // Shadowing ==> Re-declaration var x; // re-declared // let y; console.log(x); console.log(y); console.log(z); =============================================== Q: CAN WE DECLARE A GLOBAL VARIABLE INSIDE THE FUNCTION? ======================================================== yes. "window" object ==> browser "use strict"; function f1(){ window.x = 123; window.y = 321; document.write("x = "+x); document.write("y = "+y); } function f2(){ document.write("x = "+x); document.write("y = "+y); } f1(); f2(); ====================================================== Q: Do we modify the constant? ============================= Yes, only dynamically. "use strict"; // const z = 123; // console.log(z) // z = 321; // console.log(z) const name = prompt("Enter your name:") console.log("Hi"+name);