diff --git a/README.md b/README.md index 924a02f7..5f4a9c59 100644 --- a/README.md +++ b/README.md @@ -333,6 +333,9 @@ Lexical Scope: Refers to how variable access is determined based on the physical - [JavaScript Block scope vs Function scope - nivek](https://www.youtube.com/watch?v=IaTztAtoNEY) - [Lexical scoping in javascript - Hitesh Choudhary](https://www.youtube.com/watch?v=qT5S7GgIioE) - [Modern Scope Handling in JavaScript (ES6 and Beyond) -Prashant Dewangan ](https://www.youtube.com/watch?v=zMseUdOR7z8) +Additional Resource:- 📑💻 +- [Global and local scope in javascript | chai aur #javascript -Chai Aur Code ](https://youtu.be/cHHU0jXfjKY?si=jbMks9iRauJCYygC) +- [Lexical scoping and closures in javascript | chai aur #javascript -Chai Aur Code ](https://youtu.be/VaH09NXQZ58?si=eTX90Y81GvccMB_g) **[⬆ Back to Top](#table-of-contents)** @@ -848,6 +851,35 @@ The Event Loop is a critical part of JavaScript's concurrency model, ensuring no - [CLOSURES en JavaScript: Qué son y cómo funcionan - Carlos Azaustre](https://youtu.be/xa8lhVwQBw4) - [Learn Closures In 7 Minutes - Web Dev Simplified](https://www.youtube.com/watch?v=3a0I8ICR1Vg) + Here is a simple but efficient eample of javascript closures (Code):- + + //Counter function here + + function createCounter(){ + let count = 0; //private variable, can't be accessed outside this function + return { + increment: function(){ + count++; + console.log(`Count after increment: ${count}`); + }, + decrement: function(){ + count--; + console.log(`Count after decrement: ${count}`); + }, + getValue: function(){ + return count; + } + }; + } + + // now using this + const counter = createCounter(); // holding the function in another variable + counter.increment(); // Count after increment: 1 + counter.increment(); // Count after increment: 2 + counter.decrement(); // Count after decrement: 1 + console.log(`Final count: ${counter.getValue()}`); // Final count: 1 + + Wrapping up:- The data (here count) remains persistent and protected, while still it can be maipulated through controlled functions **[⬆ Back to Top](#table-of-contents)**