Closures Explained in 100 Seconds // Tricky JavaScript Interview Prep

Closures Explained in 100 Seconds // Tricky JavaScript Interview Prep

Fireship

3 года назад

337,524 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

The Ultimate Shield
The Ultimate Shield - 17.06.2021 19:41

click the f**king button man its his f**king dream😂😁😂

Ответить
Sk8nRock
Sk8nRock - 17.09.2023 15:28

If you ever find yourself faced with a question like this, the best thing you can do is to walk away and not waste your time with such interviews, because they might as well ask you what is the output of: "b" + "a" + +"a" + "a" and other "tricky" questions like this.

Ответить
TheIDK
TheIDK - 08.09.2023 01:21

Took me an hour to fully understand a 5 minute video, but I got it

Ответить
SPAAAAACE
SPAAAAACE - 08.09.2023 01:14

What a legendary video. It took me around 10 minutes of pausing and rewinding just so I could absorb the information.

Ответить
Jomin Jose
Jomin Jose - 22.07.2023 20:39

This is the best video on closure

Ответить
Arghya Guha
Arghya Guha - 18.07.2023 15:27

This is prolly the best explanation of closures there is!

Ответить
goGGled tech
goGGled tech - 12.07.2023 09:48

ok

Ответить
115breno
115breno - 07.07.2023 11:38

am i getting it right? it involve async and global variables. the setTimeout ques up something and the loop keeps running, so by the time setTimeout runs log, 'i' has changed to 3. so then wouldn't that meen 'i' in the closure is just a reference? why

Ответить
Alex Afoko
Alex Afoko - 17.06.2023 17:30

who else didn't understand a word

Ответить
Adil Mughal
Adil Mughal - 18.05.2023 16:23

Can't wrap my head around it.

Ответить
ahmed mohmed
ahmed mohmed - 24.04.2023 20:11

Great Video !!

Ответить
de Laplace's
de Laplace's - 19.04.2023 05:01

quick Q why 3 not 2 ?

Ответить
BeBrave
BeBrave - 18.04.2023 12:35

Why var i return 3 three times (3,3,3)? Why not only 1 time when it reached the end of for loop? How it keeps repeating the loop 3 times?

Ответить
Carlos Eduardo Magalhães Pereira
Carlos Eduardo Magalhães Pereira - 05.04.2023 23:45

awesome explanation as always!

Ответить
Vaibhav Desai
Vaibhav Desai - 31.03.2023 12:50

Buy using Var also, we are able to print 0,1,2 but fr that we need to write another function, and put them inside in it, var is global variable, so they pointing to same memory location, that's the reason, so we just need to change that position, when everyone loop iterate, so for that we need closure, that definitely solve the problem.

Ответить
Sen ZmaKi
Sen ZmaKi - 27.03.2023 13:48

So basically the closure takes a snapshot of the value of i every time it's called

Ответить
Sen ZmaKi
Sen ZmaKi - 27.03.2023 13:48

So basically the closure takes a snapshot of the value of i every time it's called

Ответить
lachi dong
lachi dong - 21.03.2023 03:51

can you explain call stack please

Ответить
Ermia Amirabadi
Ermia Amirabadi - 12.03.2023 14:49

thanks

Ответить
Obele Kingsley
Obele Kingsley - 09.02.2023 10:32

It looks like you were sick when recordings video because ua are usually fast

Ответить
Saturn_TeaTree
Saturn_TeaTree - 08.02.2023 16:54

Looks like react to me

Ответить
Daniel S. Espitia
Daniel S. Espitia - 12.01.2023 17:42

Hey Fireship! Great video, What's the extension you're using to console.log the values within the IDE next to the console.log statement?

Ответить
Gelişmiş Suriyeli
Gelişmiş Suriyeli - 08.01.2023 16:24

I laughed outload like a crazy person in the office when he said "But this is gonna be very difficult if you don't click the f***ing button"

Ответить
Sdiri mohamed salah
Sdiri mohamed salah - 01.01.2023 04:33

I still don’t know exactly how JavaScript works I couldn’t understand when you passed the 3 as arg how this value transferred to (num) in the arrow function 😮

Ответить
Norbertsson
Norbertsson - 24.12.2022 02:41

I'd kick out anyone from my company if they'd use var ever. I don't care if you are one of 5000 devs and accidentally know how it works, it's just NO GO for me.

Ответить
ronald tamparong
ronald tamparong - 11.12.2022 07:41

talking too fast

Ответить
re::liable
re::liable - 30.10.2022 20:24

First thing I'll mention is why are they using `var`. I'd avoid it like the plague. 😂 It introduces unnecessarily unintuitive problems (like this maybe?)

And then I'll explain it like so:
With `var`, the variable `i` actually lives "outside" the loop. Every iteration of the loop creates a new function `log`, but all of them references the same "outer" `i` variable. What's logged depends on when the loop finishes. If it's done by 100ms, and it'll likely will be, then the `i` variable will be 3 by then, and all the timed-out functions are going to log it. If for some reason it isn't then the callback will log whatever is the `i` value at that time

With `let`, every iteration gets its own "copy" of `i`. And the `log` functions use their corresponding copies of it. Each iteration has its own "enclosure" of state/variables/values. It doesn't matter if the execution of functions within them are deferred for a later time; they will still refer to the values within the same "enclosure".

Ofc I do not know if that is fully correct 😂 I feel like it's mostly right though. Also I think I've read somewhere that "closure" !== "enclosure"

Ответить
Dylancougar
Dylancougar - 29.10.2022 20:28

Jesus Christ I legtitimately thought I had this figured out until the hoisting part, and the fact that it logs 3 3 3, instead of 2 2 2 when using var.

Ответить
vineet sajwan
vineet sajwan - 10.10.2022 17:25

📌

Ответить
Andrew N
Andrew N - 08.10.2022 14:38

So closures is just global variables? I dont understand

Ответить
Iman Hosseini
Iman Hosseini - 01.10.2022 17:40

OrganicChemistryTutor of programming u great ser

Ответить
Nbtwall
Nbtwall - 12.09.2022 23:07

Is this where clojure gets its name from, or no?

Ответить
2herzog
2herzog - 07.08.2022 14:44

i dont get it it increments from 0 to 3 and then runs the for loop?

Ответить
The AalPal
The AalPal - 05.08.2022 01:44

Can someone explain why it logs 3 from the beginning? I didnt get how the loop is supposed to run 3 times, but global i runs 3 times despite i being not less than 3 anymore

Ответить
jennisonb37
jennisonb37 - 20.07.2022 03:19

Why does it return 3, 3, 3, instead of 2, 2, 2 since the iterator is to i < 3?

Ответить
Clash Clan
Clash Clan - 15.07.2022 20:35

Why just declaring functions in an object/class consume memory when compiled code don't have problem like this for eg.C#.

Note: i am asking about functions without prototype in javascript compared to C#, java, etc

Ответить
Miss Sincere
Miss Sincere - 21.06.2022 02:18

Imma have to watch this again a few times cause, I don’t get it lol 😂

Ответить
Alex Bhattarai
Alex Bhattarai - 20.06.2022 19:27

I recently gave an interview for a company. I was asked the same question. I wish I had watched this video before.

Ответить
Krzysiekoy
Krzysiekoy - 10.06.2022 03:48

Some people in the comment suggested that you could still use var and log 0, 1, 2 by doing IIFE (immediately invoked function expression) and that is a common solution when you are forced to use var (for example, during an interview).

I've been coming back to this video from time to time as my understanding of JS improved, and I have found another interesting solution with var while exerimenting:

for (var i = 0; i < 3; i++) {
function log(){
console.log(this);
}

setTimeout(log.bind(i), 100);
}

So, basically we create a bound log function, and we pass the value of i variable in the current iteration as "thisArg".

Ответить
Computeshorts
Computeshorts - 06.06.2022 17:16

Been trying to understand closures for ages and this video taught me them in 5min well worth it. Thanks.

Ответить
Mount Everest
Mount Everest - 30.05.2022 22:42

Just when I thought I knew JS😭

Ответить
Koy Foster
Koy Foster - 26.05.2022 21:17

3 years writing JS and I never took the time to look up the diff between let and var. Thank you very much.

Ответить
Aaron Omary
Aaron Omary - 12.05.2022 00:59

"Javascript closures are functions that can access values outside of their own curly braces" Can't all functions do this? Like if you have a global variable, then all of your functions are closures?

Ответить
Prince
Prince - 29.04.2022 14:58

But if the value of var i = 3; How is this (i < 3) condition being fulfilled ? Please explain me

Ответить