Learn Closures In 13 Minutes

  Рет қаралды 71,393

Web Dev Simplified

Web Dev Simplified

Күн бұрын

Пікірлер: 97
@usmanquickcode
@usmanquickcode 5 ай бұрын
I just gave my interview and in this interview they asked me about closures. which ofcourse i have no idea. I have used it but no idea what is called. and i open my youtube and boom you are there. really like the simple explaination.
@anothermouth7077
@anothermouth7077 5 ай бұрын
Those know-it-all interviewers always play with these gotcha questions which are not even practical anymore
@looming_
@looming_ 5 ай бұрын
@@anothermouth7077closures are pretty important tho
@youarethecssformyhtml
@youarethecssformyhtml 5 ай бұрын
Such useless interview questions. They really need to ask about the job instead of stupid shit that you're not gonna use 95% of the time
@jessecalato4677
@jessecalato4677 5 ай бұрын
​@thecoolnewsguy you will use closures constantly though, and terminology is important for proper communication...
@youarethecssformyhtml
@youarethecssformyhtml 5 ай бұрын
@@jessecalato4677 I know. Yes closures are used extensively but asking about the var gotcha in a loop is just stupid like who's gonna use var in 2024 for a new code?
@RCSoftMedia
@RCSoftMedia 5 ай бұрын
I use one type closure all the time, combined with promises, when I have to execute a function on an array of data asynchronously, all you have to do is return a promise with a function, inside create a couter at 0, create the function thats going to process an entry, before return, incremente the counter, and excecute the inner function again, when its done, resolve the promise.
@mewansynniang1902
@mewansynniang1902 4 ай бұрын
Beautifully explained
@joesharp3580
@joesharp3580 3 ай бұрын
Just incredible video as always. Somehow you always get to the absolute core of a concept and then demonstrate it beautifully
@WoShouji
@WoShouji Ай бұрын
The Closure actually can be seen. It is simple and complex ))) Basically it is an internal OBJECT. There is always a Scope Chain: an array with elements: [[Scopes]]. 1. Scope Chain and [[Scopes]] Array: The [[Scopes]] array (seen in DevTools as Scopes) represents the scope chain of a function. It contains objects that correspond to different scopes, including Global, Script, and any function-specific scopes. This scope chain allows the JavaScript engine to resolve variables as the function executes. 2. Presence of the Global Object: Global object is always included in the scope chain and is usually the last element in [[Scopes]]. It contains globally defined variables, functions, and objects (like window or document in browsers). 3. Creation of a Closure Object: When you define a nested (or child) function inside another function, a closure is formed. This creates a Closure object in [[Scopes]] that preserves any variables from the outer (parent) function that the inner function might need. The Closure object becomes the first element in [[Scopes]] for the inner function. The Global scope moves down to a later position, allowing the child function to access both the outer function's variables and the global variables. 4. Persistence of Variables After the Parent Function Ends: Even if the parent function has finished execution, the variables it defined will be preserved by the closure as long as the inner function (closure) remains accessible somewhere in the code. This is what allows nested function to retain access variables of parent function and even change them when nested function called again and again and again. 5. Additional Scopes (e.g., Block Scope): If there are additional block-scoped variables (for example from "for (var i = 0; i < 3; i++) {...}" loop), they can appear in the [[Scopes]] array as a Block scope, though this only applies to let and const. Variables declared with var would not create a new block scope, as they have function scope (Global) rather than block scope. So, 6. the Definition of Closure: A closure in JavaScript is an internal object that is created whenever a function (child) is defined inside another function (parent). This closure object allows the child function to retain access to the variables in the parent's scope, even after the parent function has completed execution. The closure is represented in the [[Scopes]] array as a Closure object, holding references to the outer function's variables that the inner function may use.
@xzex2609
@xzex2609 5 ай бұрын
React vue and angular use closures all the time , all functional components are closures
@maciejzettt
@maciejzettt 5 ай бұрын
As well as state management is
@Regeneration1996
@Regeneration1996 5 ай бұрын
And react hooks to 😅
@xzex2609
@xzex2609 5 ай бұрын
@@Regeneration1996 of course in the end a hook will return some functions as objects and states too , when I was said react I mean most of it uses closures
@mhadi-dev
@mhadi-dev 5 ай бұрын
Best used in Factory design pattern.
@PavloBozhok
@PavloBozhok 5 ай бұрын
Thanks a lot, I know and remember how closure works in javascript, but I never knew about something tricks.
@himalayagupta7744
@himalayagupta7744 5 ай бұрын
I had doubts for this last example, so I asked gpt to explain it to me deeply. It errored the first few times but at last gave the correct answer which claude also verified. Yes, for each iteration of the loop, a new scope is created, and within that scope, a new binding for the variable i is created. Here’s a detailed breakdown: First Iteration (i = 0): A new scope is created. A new binding for i is created and initialized to 0. The setTimeout callback captures this binding, so it logs 0. Second Iteration (i = 1): A new scope is created. A new binding for i is created and initialized to 1. The setTimeout callback captures this binding, so it logs 1. Third Iteration (i = 2): A new scope is created. A new binding for i is created and initialized to 2. The setTimeout callback captures this binding, so it logs 2. In each iteration, the let declaration ensures that i is re-declared and re-initialized within the new scope. This results in three separate bindings for i, each one unique to its iteration.
@matheusneves688
@matheusneves688 5 ай бұрын
Thank you! Amazing content!
@captkalik
@captkalik 4 ай бұрын
That for loop let var closure interview question is just brutal
@jovi9918
@jovi9918 5 ай бұрын
Great quick watch
@karimkrimou399
@karimkrimou399 5 ай бұрын
Excellent and really clear approch and explantion
@lesalmin
@lesalmin 5 ай бұрын
This is the best explanation of closures I've seen (including your own previous explanations).
@TalesGrechi
@TalesGrechi 5 ай бұрын
Great video!
@prince5922
@prince5922 Ай бұрын
MDN particularly mentions that JS automatically creates closures everytime you create a function. So the first example actually IS A CLOSURE.
@AlejandroPoleo18
@AlejandroPoleo18 4 ай бұрын
❤ love it
@QuanTran-wt4yt
@QuanTran-wt4yt 5 ай бұрын
Can you make a video talking about message queue?
@crakyanime8903
@crakyanime8903 5 ай бұрын
it would be good if you cover conditional scope or conditional closures
@saurabhshrivastava5383
@saurabhshrivastava5383 4 ай бұрын
Hello Kayal, In your second example for closuers "function elementCreator(element)", if we directly return document.createElement(element) instead returning a function then what difference will it make, I think direct return will be better, what you suggest.
@Kingreey
@Kingreey 4 ай бұрын
Excelent
@LifeEssentials22
@LifeEssentials22 25 күн бұрын
Please I need a video on must know backend interview questions, web services and websocket
@doniaelfouly4142
@doniaelfouly4142 5 ай бұрын
Thanks
@Mari_Selalu_Berbuat_Kebaikan
@Mari_Selalu_Berbuat_Kebaikan 5 ай бұрын
Let's always do alot of good ❤
@manpreetjord1885
@manpreetjord1885 5 ай бұрын
Great video as usual. But Kyle, your 1x Playback speed feels like 1.25x bro. 🙂 Interviewers also ask that how you will print 0,1,2 when using var i=0; . We have to use the concept of closure here.
@tomersimcha
@tomersimcha 3 ай бұрын
you can do something like this and it works: for(var i = 0; i < 4 ; i++) { const func1 = (i) => { setTimeout(()=>{ console.log(i); },1000); } func1(i); } in this way you use closures to capture the value of i on each iteration even if you use var
@tommysmith5479
@tommysmith5479 5 ай бұрын
So, if I can sum up and get confirmation: a closure is where an outer function or scope is not garbage collected because there is an inner function that relies on the outer function or scope's variables... is this correct?
@plamenyordanov131
@plamenyordanov131 4 ай бұрын
Printing 3 times 3 has more to do with the event loop than closures
@jakubliska4730
@jakubliska4730 3 ай бұрын
both actually but ok, he ommited event loop becouse it's unrelated
@viallymboma9874
@viallymboma9874 5 ай бұрын
In fact the concept of react custom hooks relies on the notion of closure When you're building a react custom hook, you're actually using closure.
@virtual5754
@virtual5754 5 ай бұрын
What you describe is just oop, but with extra steps
@iftekharhaidar2397
@iftekharhaidar2397 5 ай бұрын
Dear, could you please explain what is JavaScript reference memory?
@alexandraweather4156
@alexandraweather4156 5 ай бұрын
Thank you for this video and the job you do, but what is a JavaScript Developer? The only thing i could find is "JS developer" - in react, angular, vue and stuff Is there atually a job for JS developer in JS?
@dfields9511
@dfields9511 5 ай бұрын
Pascal lets you do this as a well, never used that feature of pascal. But was forced to Learn it
@mcRidersEvolution
@mcRidersEvolution 5 ай бұрын
What is the purpose of returning function which returns element and not returning element with the first function?
@Pareshbpatel
@Pareshbpatel 5 ай бұрын
JavaScript Closures, bautifully explained. Thanks, Kyle {2024-07-09}
@siasquad7360
@siasquad7360 5 ай бұрын
Sir please explain with the help of draw and after that code
@alienews0
@alienews0 4 ай бұрын
5:00 nope u didn't called your function on that line, u declared it ; u called it on the last line : return func2
@green-coder-clj
@green-coder-clj 5 ай бұрын
I misread the title as "Learn Clojure in 13 minutes" 😅 Clojure & Clojurescript indeed help landing a job for the FE.
@consoledoterror971
@consoledoterror971 5 ай бұрын
I think the explanation for var, let with for loop is a bit wrong. 🤔
@amiryankolovich8560
@amiryankolovich8560 5 ай бұрын
I ran the code with the var and got 0,1,2 🤔
@amiryankolovich8560
@amiryankolovich8560 5 ай бұрын
Oh its because i had an error few lines before this I guess its js weird engine
@AlThePal78
@AlThePal78 5 ай бұрын
I honestly thought that it didn't call function too until it said return function to and since the variable was already age 30, that is why it started with 30. What if you called the function first?
@ironCheval
@ironCheval 5 ай бұрын
Does it keep the dependant variable and delete the old ones? E.x. when you added age = 30, will age = 29 be kept still by javascript?
@MbzMOKKA
@MbzMOKKA 5 ай бұрын
No because it's the same memory spot. In his example, when he does age = 30, it overwrites the value 29 in variable 'age', instead of creating a new variable.
@ajiteshmishra0005
@ajiteshmishra0005 5 ай бұрын
Hii brother... You explained let and var concept in for loop. But still I'm confused. Can you explain in some other way.
@24306529
@24306529 5 ай бұрын
dude be selling 10 other videos within 1 video
@LifeEssentials22
@LifeEssentials22 25 күн бұрын
Also redix
@sealsharp
@sealsharp 5 ай бұрын
I was just wondering, is it possible to see the what structures&code JavaScript actually creates? Don't know how to call it. In C#, you can see the different stages ( C#, IL, ASM ) and that makes perfectly clear how closures technically work.
@letsbeafraid2233
@letsbeafraid2233 5 ай бұрын
JavaScript doesn't create code it gets JIT compiled into machine code directly by the engine. Read about V8 and how it works
@supernovaw39
@supernovaw39 5 ай бұрын
Unrelated but why are you running VS Code as Admin? 🤔
@DanielBrownsan
@DanielBrownsan 4 ай бұрын
I genuinely thought I had the playback speed set to 150-200% for the first 3 minutes of the video.
@emmanuelameyaw7
@emmanuelameyaw7 3 ай бұрын
Is there a real world use for it??
@aqibmalik6313
@aqibmalik6313 5 ай бұрын
first comment after first comment of arunkaiser😂😂
@johnpatrickoldfield534
@johnpatrickoldfield534 5 ай бұрын
Do you know how to do that integer story with a public bank website inside a private login inside page two with a currency and interger value where I need to insert a legal agreement account balance update from 0 => R 1 000 000 Zar ? It’s a public https web address?! The javascript wipes the direct insert after page refresh how do I make the interger value const? Do you have team viewer and want to try and or show me?
@JacobZigenis
@JacobZigenis 5 ай бұрын
Hate to break it to you, but absolutely no one is going to help you with manipulating a bank website. Further, what you're seeing isn't just javascript wiping the value. It's retrieving the value from the bank's servers, which you cannot modify no matter how much you manipulate the page. Gotta study up a lot more and stop asking for advice on malicious activities on KZbin. Also, on the off chance you do manage to do something malicious, you will almost certainly get caught.
@johnpatrickoldfield534
@johnpatrickoldfield534 5 ай бұрын
@@JacobZigenis my laptops storage is full and not booting at the moment so I cannot screenshare. I don't see how it's my problem the external and Internal banking staff cannot manually show me howto insert credits into bank tables in cobol or whatever the language is from the email pseudocode to the browser web security layers in the osi model so that I may make debits to pay attorney legal fees for bank account topups. I think it's worth a trial investigation ie look, screenshare, record discuss, etc and a look into the file and code framework on the Javascript and in the console just for a deeper explination, because I just know sql server, table updates, hidden servers, html, Javascript and css. The actual coding language and topology to see exactly where the update changes are made. Worse case the system gets reported and then gets fixed. I don't think it's too complicated less than 1000 lines of code plus minus with like 400 lines on my screen at once with like 30 lines each side of the zero balance variable.
@MichaelKire
@MichaelKire 5 ай бұрын
Fun fact. Classes in JS is actually just syntactic sugar for a closure with a special constructor/prototype function inside
@charleschukwuemeka8482
@charleschukwuemeka8482 5 ай бұрын
While watching the video, this came to my mind... It's nice seeing that someone else thought of the same thing
@trombecher
@trombecher 5 ай бұрын
A class is syntactic sugar for a function, not necessarily a closure. A function becomes a closure when capturing variables from the parent scope, not because it’s a class.
@trombecher
@trombecher 5 ай бұрын
If you have a global function that captures a variable from the outer (global) scope, then this function is a closure, because a closure is a function that captures variables from the parent scope. Even if you are calling console.log, because console is a variable in the global scope, you are capturing a variable and therefore your function is a closure. The only functions in JavaScript that are not closures are pure functions. Please do some research before making a 13min video about a topic.
@looming_
@looming_ 5 ай бұрын
Really makes me wanna buy his “course” xd
@Efecretion
@Efecretion 5 ай бұрын
Closure to scoping is like 0/1 to binary -- closure is how scoping is implemented in JS, while 0/1 is how binary is represented typically.
@Uncaught_in_promise
@Uncaught_in_promise 5 ай бұрын
Please create Next js e-commerce project catalog with multiple checkboxes filtering, sorting, pagination, search. More e-commerce realistic. Thank you.
@bobwilkinsonguitar6142
@bobwilkinsonguitar6142 5 ай бұрын
I think you should do it!
@Psai4456
@Psai4456 3 ай бұрын
one guy asked me in interview i told the wrong answer
@jovincebrillantes1042
@jovincebrillantes1042 5 ай бұрын
So they're called closures, well I'll be dammed. I've been calling them higher order functions all this time.
@rico454
@rico454 5 ай бұрын
They are kinda the same thing. Higher order functions return a function and usually, the function that is returned has access to variables inside the original function which is what closures is. So yeah different concepts but essentially the same thing
@RayZde
@RayZde 5 ай бұрын
This is not true for all programming languages. JavaScript is an anomaly.
@arunkaiser
@arunkaiser 5 ай бұрын
First comment
@aniketroy03
@aniketroy03 5 ай бұрын
autograph pls 😂
@Pacvalham
@Pacvalham 5 ай бұрын
🥇
@AchwaqKhalid
@AchwaqKhalid 5 ай бұрын
Dude increase your *FONT* size for tutorials at least 💡
@ahmatmhtlouky4179
@ahmatmhtlouky4179 3 ай бұрын
Man😂 after each five words you link as to another video 🤦🏽‍♂️
@zfolwick
@zfolwick 5 ай бұрын
god I hate javascript
@jasonsworld333
@jasonsworld333 5 ай бұрын
Too bad the jobs are all taken lol
@anothermouth7077
@anothermouth7077 5 ай бұрын
In Same boat 😂
@alpaca_growing_kit
@alpaca_growing_kit 5 ай бұрын
If you work with React you desperately need to understand closures. I have run into some insane React bugs that were created because of closures in complex functional React.
@alpaca_growing_kit
@alpaca_growing_kit 5 ай бұрын
HINT: Watch out for modifying an onChange when its getting prop drilled. Another reason to create context hooks as often as possible...
@looming_
@looming_ 5 ай бұрын
Fact that this dude didn’t mention react once makes me question his understanding. Also the fact he messed up the for loop example.
@servertimeout8371
@servertimeout8371 4 ай бұрын
no semicolons 😮 you would never get a job from me
@b1studio648
@b1studio648 5 ай бұрын
Why u move ur head too much? Can you hold your head still while recording or move less. I would appreciate it. The way u move your head makes me dizzy
@logimw
@logimw 5 ай бұрын
why your head bouncing so much? it's very disracting no matter how valuable content you show (always very valuable)
@AdolfRizzler41
@AdolfRizzler41 5 ай бұрын
Do a video on booty sizes
@abcdabcd8605
@abcdabcd8605 5 ай бұрын
What does that even mean?!!
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 491 М.
Chain Game Strong ⛓️
00:21
Anwar Jibawi
Рет қаралды 33 МЛН
Farmer narrowly escapes tiger attack
00:20
CTV News
Рет қаралды 15 МЛН
Learn HTML in 1 hour 🌎
1:00:00
Bro Code
Рет қаралды 2,3 МЛН
Learn DOM Manipulation In 18 Minutes
18:37
Web Dev Simplified
Рет қаралды 1 МЛН
JavaScript Closures Tutorial (Explained in depth)
19:03
ColorCode
Рет қаралды 76 М.
The CSS Display Property is Changing Forever
15:20
Web Dev Simplified
Рет қаралды 49 М.
Only The Best Developers Understand How This Works
18:32
Web Dev Simplified
Рет қаралды 114 М.
If You Cannot Name All 5 JS Scopes You Need To Watch This Video
15:06
Web Dev Simplified
Рет қаралды 61 М.
Why "Stale Closure" Misleads React Devs
12:37
UI Engineering
Рет қаралды 3,3 М.
Learn TypeScript Generics In 13 Minutes
12:52
Web Dev Simplified
Рет қаралды 289 М.
JavaScript Visualized - Closures
11:34
Lydia Hallie
Рет қаралды 55 М.