Solving JavaScript's Most NOTORIOUS Interview Question

  Рет қаралды 673,201

Conner Ardman

Conner Ardman

Күн бұрын

Пікірлер: 403
@HelloThere-xs8ss
@HelloThere-xs8ss Жыл бұрын
If this is an interview question, I'm walking out the door and finding a better place
@NaN-se8ye
@NaN-se8ye Жыл бұрын
Kekw, not knowing basics of that horrendous language is like programming in C depending only on compiler
@HelloThere-xs8ss
@HelloThere-xs8ss Жыл бұрын
@@NaN-se8ye ok. thats why gdb exists though. those js questions arent scary, they are silly. and any company that asks silly questions is a silly company.
@FzsHotDogInDonut
@FzsHotDogInDonut Жыл бұрын
you don't want free money?
@ko-Daegu
@ko-Daegu Жыл бұрын
@@HelloThere-xs8ss ok you can walk out all interviews then good luck if every time you get somerhitn you belive is silly you just walk out good luck running any business or any type pf relations really
@HelloThere-xs8ss
@HelloThere-xs8ss Жыл бұрын
@@ko-Daegu why did you take my statement personally?
@qTnationX
@qTnationX Жыл бұрын
A programmer puts two glasses on his bedside table before going to sleep. A full one, in case he gets thirsty, and an empty one, in case he doesn’t
@sh-ehmed
@sh-ehmed Жыл бұрын
...and a prescription one, in case he can't get that bug outta his head.
@faheemrhodajackson2470
@faheemrhodajackson2470 Жыл бұрын
I thought you were referring to spectacles.
@Jackson_Zheng
@Jackson_Zheng Жыл бұрын
😂
@keifer7813
@keifer7813 Жыл бұрын
I don't get it.
@tyjhug
@tyjhug Жыл бұрын
😂😂😂😂😂😂😂
@abdoufma
@abdoufma Жыл бұрын
Using `var` in this example is the equivalent of declaring a global variable (var i = 0) right before the loop. setTimeout's are only called after the synchronous code finishes executing, (ie, after the loop has finished, and i has reached 3). the console.log(i) inside of them prints 3 because the 'i' they have access to is the one in the global scope, which has a value of 3. Using let on the other hand is equivalent to having a (let i = 0/1..../n) statement on each iteration of the loop. Since every iteration has its own, local declaration of the variable i, the setTimeouts here get access to different (i)s, hence the different values. this questions tests your knowledge on two key JS concepts : variable scopes/hoisting, and the event loop
@akb4076
@akb4076 Жыл бұрын
Still not clear, set time out to 10 sec for let, if it is a sync call how does it pints 012, for loop has already finished before time out.
@abdoufma
@abdoufma Жыл бұрын
​​@@akb4076the `setTimeout()` calls are what's called a Macro task. Macro task are *not* executed synchronously, instead, they are added to a queue. The tasks in this queue are executed when *_and only when_* the call stack (ie the one that contains synchronous code) is empty.

So even if the loop took 10 minutes to finish executing, the first setTimeout is only gonna be executed after that, since it’s a macro task. Now, the reason the console.log’s print the correct values in the let example, is because the setTimeouts are `scheduled` to run with local values of i, It doesn’t matter when they’re going to execute their callback function, because that function has already been `configured` to run with the correct value of i (0, 1, and 2 respectively) The only reason the `var` example prints 3 is because `i` in this instance is a global variable, there’s no `configuring` being done, the callback functions are just told to print the value of a global variable, and that global variable happens to have a value of 3. For more on the event loop, I highly recommend watching this talk -> kzbin.info/www/bejne/bpKqmY2HoNR5mLM
@Никита-р1в5и
@Никита-р1в5и Жыл бұрын
Thanks for the explanation, since the video literally explains nothing.
@SD_7878
@SD_7878 Жыл бұрын
​@@abdoufma that was a great explanation!
@dabbopabblo
@dabbopabblo Жыл бұрын
@@Никита-р1в5и The video explains it better, you're just too new to understand the terminology he used. Don't criticize someones intellect because your own lack of understanding. That's ironic at best and sad at worst.
@Beastintheomlet
@Beastintheomlet Жыл бұрын
Pro tip: don’t use var, use let. There’s never a good reason to use var.
@NathanHedglin
@NathanHedglin Жыл бұрын
Let doesn't work in IE11 . . .dont ask how i know 😂 😭
@Bozzix994
@Bozzix994 Жыл бұрын
@@NathanHedglin just stop supporting IE11 in 2023. It's support from Microsoft stopped on June 15th 2022 ;)
@sfselectron
@sfselectron Жыл бұрын
@@NathanHedglin No one used IE11 except for grandparents
@programming.jesus1234
@programming.jesus1234 Жыл бұрын
var was significantly more performant than let for a while. Pro tip: There are no pro tips with anything js/ts, at the end of the day they are shit languages, try to avoid anonymous/arrow/lambda/closures also, they are heap allocated, that is shitty. Most javascript devs don’t know what the heap is unfortunately, but basically heap is O log n stack is O 1
@NonexistentCZ
@NonexistentCZ Жыл бұрын
Let doesn't allow you to redeclare global values, which is done quite commonly when dealing with modules or other global dependencies, please don't spread "pro tips" when you don't understand a feature. var module = module || {}; // let would crash here
@RyanLilleyman
@RyanLilleyman Жыл бұрын
For anyone confused, let creates new variables in memory for each iteration. i is not the same i for each iteration. This is why the closures 'remember' different i values. For var, var is not being recreated for each block and iteration. It's value is just getting updated from 0 to 3 (due to last increment when i = 2)/ It's also important to remember that due to the asynchronous nature of setTimeout. closures won't run until after 100ms. However, the for loop will complete before that 100ms. This allows the closures to ONLY refer to the changed var variable, not their newly created i's when let is used.
@4t-_
@4t-_ 11 ай бұрын
You need to be top cmnt. Good explanation.
@yeybaby6778
@yeybaby6778 10 ай бұрын
Thanks i understand it now
@louisfrancisco2171
@louisfrancisco2171 5 ай бұрын
Even if the loop takes more than 100ms, setTimeout will not execute until the current function is finished.
@lavaa7392
@lavaa7392 3 ай бұрын
Best explanation I’ve seen in these comments
@rupeshkadam5544
@rupeshkadam5544 Жыл бұрын
your answer was more complicated than than the question
@Stefan-xm9qb
@Stefan-xm9qb Жыл бұрын
I had to google set timeout and that it is asynchronous. Then I went back and watched the explanation again and now I understand.
@siriusgaming3080
@siriusgaming3080 Жыл бұрын
@@Stefan-xm9qb Thanks bro. yes, you explained better.
@aliefdanyseventri1010
@aliefdanyseventri1010 3 ай бұрын
var is hoisted to the nearest enclosing scope which is the global scope (from the example in the video). var also has a special behavior of reinitialization meaning for each loop it does not create a new variable but instead it modifies the already declared variables which is this case 'i' now because the closure that created inside set timeout calls accessing 'i' which is declared using var, all of 3 closure actually accessing same variable (literally same variable from memory, not just its name 'i'). and because the executions itself is deffered using timeouts, when that 3 callbacks is actually being executed, it will read the latest value of 'i' which is the value of integer 3. thats why we saw the ouputs of 3 3 3 in the video
@yisus.avocado
@yisus.avocado Жыл бұрын
I saw "var" and immediately went 💀
@TheJuankpunk
@TheJuankpunk Жыл бұрын
As a beginner and not native English speaker I had to watch it like 6 times but I get it now. Thank you! Great problem to understand the difference between let and var
@yasinvarol_
@yasinvarol_ Жыл бұрын
Same here 😭
@NOBODYxx09
@NOBODYxx09 Жыл бұрын
Count me in 👋💀
@kenchap_
@kenchap_ Жыл бұрын
This makes minimal sense to me, but I have no doubt that this guy is helpful to the people who are into this field. Clear spoken, good pace, good quality. Keep going man!!
@bigsmoke1284
@bigsmoke1284 Жыл бұрын
minimal sense? why's that?
@kenchap_
@kenchap_ Жыл бұрын
@@bigsmoke1284 I’m not super big into coding so I couldn’t fully experience his teaching. But I have no doubt in my mind he’s saving some peoples jobs right now 😂
@bigsmoke1284
@bigsmoke1284 Жыл бұрын
i see. yes, i agree with you. this is definitely a more advanced concept within javascript/programming itself. this is most likely to be a question in mid/senior interviews.
@sedhunique
@sedhunique 5 ай бұрын
Var hosits the variable to global... So after 100 ms the var will be 3 (as loop would be finished) When u use let.. it creates a closure.. so the i is binded to the setTimeout..so even if u set Timeout for 1 hour .. it will remember the state of i
@MahaKhanMK
@MahaKhanMK Ай бұрын
Got asked this question just today and I was already looking for answers. Thank you.
@Tigregalis
@Tigregalis Жыл бұрын
most sane programming language
@mosomiliardaru
@mosomiliardaru Жыл бұрын
most insane comment
@yuriydalekyy6453
@yuriydalekyy6453 5 ай бұрын
Also possible solution for(var i = 0; i{console.log(val)}, 100, i) }
@AnkurShah_CS
@AnkurShah_CS Жыл бұрын
Very informative! Thank you so so much! :)
@BrinleyBlogette
@BrinleyBlogette 11 ай бұрын
My previous test lead does this in interviews. I wouldn’t comment like you’re above this. The simplest way to find if a candidate is real or bs is by asking list of questions that go from very basic to more complex. You can identify someone who is faking it very quickly with the basic questions.
@jialx
@jialx Жыл бұрын
What kind of drop kick asks a question like this in an interview?
@nomiscph
@nomiscph Жыл бұрын
Exactly my question - and noturious... uhm...
@ivan33776
@ivan33776 Жыл бұрын
Lol, I was asked this question in the interview. 😂
@ivan33776
@ivan33776 Жыл бұрын
Funny is that I was saying it will return 0,1,2, and the interviewer was insisting it would be 3,3,3. When I asked why he couldn't answer and moved to another question.
@nomiscph
@nomiscph Жыл бұрын
@@ivan33776 do you then know the answer to this let a = 1; setTimeout(() => console.log(a)) a += 1
@interstella0
@interstella0 Жыл бұрын
Knowing this shows how far your skill is in the language, python has something similar called closure late binding, but js is more out rages with that let/var difference
@tehphoenix7441
@tehphoenix7441 10 ай бұрын
This is actually present in C++ too. There you have options to copy variables or to refer to them. Later will result in a similar behavior.
@harleyspeedthrust4013
@harleyspeedthrust4013 10 ай бұрын
yes, but fortunately in c++ that kind of thing makes sense. if you write this example in c++ and capture by reference in the closure, then it's obvious that the result will be 3 3 3. in javascript, it's not at all obvious unless you know that `var` doesn't actually declare a variable where it says it does; it declares a function scoped variable - so even though you declared a variable in the for loop, that variable is scoped outside of the for loop. it's completely insane and unexpected behavior, unlike c++
@tinahalder8416
@tinahalder8416 Жыл бұрын
Yes, javascript is a good language
@nickbh15
@nickbh15 Жыл бұрын
Is this sarcasm?
@DodoLP
@DodoLP Жыл бұрын
@@nickbh15 obviously
@nalcij
@nalcij Жыл бұрын
@@nickbh15yea, js isnt very good
@PieJee1
@PieJee1 5 ай бұрын
Make sure your babel/typescript/whatever transpiler does not replace let with var which it does if you use some ancient settings
@GhostSS168
@GhostSS168 6 ай бұрын
You can also use function under function inside for loop. So that you don't need to use let.#closure
@Rei-bg3rf
@Rei-bg3rf 2 ай бұрын
If you don't know what you doing, just use let or const please 😭😭
@tylerjing5235
@tylerjing5235 Жыл бұрын
But would would it log out 3 though? Isn’t i < 2?
@eliasgeirdal676
@eliasgeirdal676 Жыл бұрын
i is incremented to 3 and thus the loop condition is no longer true and we exit the loop. However, i is still 3 and after 100ms, it will print out three 3's.
@mutlugameofhalit
@mutlugameofhalit 4 ай бұрын
@@eliasgeirdal676 so the point is here that the setTimeOut func is going to work after 100 ms and for loop is completing faster than 100 ms?
@gyanendra_chaubey
@gyanendra_chaubey 9 ай бұрын
I agree this is the most notorious problem of JavaScript. Many interviewer and interviwee know the answer but will fumble when ask to explain. 😅😅 I myself have landed in this problem during interview
@PatTheRipper
@PatTheRipper Жыл бұрын
The algorithms are getting scary good cuz we just went over this in class last night 😮
@alexanderkamenev5529
@alexanderkamenev5529 Жыл бұрын
Key point is understanding scope, closures and var, let, const. The question isn’t silly it tests how many hours you have spend with the language and so how productive of a developer you are. Yeah sure I would have more fun solving some white board problem related to graph traversing or inverting a binary search tree but let’s be honest companies look for value vs money and often jobs aren’t gonna need you to do ground breaking shit that requires big brain logic.
@icillay
@icillay Жыл бұрын
Function scope was well understood before let became a keyword, just had to do something like this: for (var i = 0; i < 3; i++) { (function(n) { setTimeout(console.log.bind(null, n), 100); })(i); }
@whitefercho
@whitefercho Жыл бұрын
Or something like this: for(let i = 0; i < 3; i++) { setTimeout(console.log, 100, i) } I wrote let 'cause it's preferable but the solution work either var or let.
@icillay
@icillay Жыл бұрын
@@whitefercho Umm, but not in JavaScript 1.1. That version cast a long and nasty and awkward shadow. Oh, how I miss her (but not really).
@whitefercho
@whitefercho Жыл бұрын
@@icillay uhm, I started this year learning javascript ES6. At least, I get why is there the method bind you applied on your solution and I liked as well. It is to use a function as an argument and there's an IIFE to make it work, am I wrong?
@GooseGumlizzard
@GooseGumlizzard 3 ай бұрын
thats horrific
@nlx8763
@nlx8763 Жыл бұрын
I think this can be used as one of those “what English sounds like to people who don’t speak English” soundtracks
@christophfischer2773
@christophfischer2773 Жыл бұрын
As a non JS Programmer, the more interesting thing to me is that setTimeout apparently doesn't block.
@akshayhere
@akshayhere Жыл бұрын
If it did, browsers would suck
@whitefercho
@whitefercho Жыл бұрын
Another way to get the iteration correctly without changing var is to use the third param on setTimeout instead of the arrow function. But yeah, its better drop var and use let
@Wentris71
@Wentris71 3 ай бұрын
Alternatively, we can create another function(new closure) with own argument “i” outside of loop and it will work just fine too
@here2code
@here2code Жыл бұрын
You sir, just got yourself a new subscriber
@shadowofheaven3279
@shadowofheaven3279 3 ай бұрын
That's why in java, to use a variable in a lambda, it needs to at least be effectively final
@Maplelicker
@Maplelicker 4 ай бұрын
Why you use settimeout inside a for loop, just for printing "0 1 2" ? Instead, you can just get rid of that settimeout, and directly use "console.log" without "setimeout"
@anujtanwar2937
@anujtanwar2937 3 ай бұрын
If interviewer says you cannot use var. Just wrap settimeout inside a function and pass I in the function call as an argument
@Namids
@Namids Жыл бұрын
That is why it is good to understand JavaScript jundamemtals... For this reason i avoid using var to avoid bugs
@qzbnyv
@qzbnyv Жыл бұрын
This was explained very well a few years back by Surma and Jake on the HTTP 203 v/podcast 🙃
@ivanrgo
@ivanrgo 10 ай бұрын
Most notorious interview question - have never seen this in my life, in fact, if I did I'd leave the room right away.
@kostiapereguda
@kostiapereguda Жыл бұрын
So let is captured using copy and var is captured using closure?
@vladostema
@vladostema 2 ай бұрын
In what practical situatuon can this be useful?
@boris29122000
@boris29122000 3 ай бұрын
Why does 'i' reach 3? Doesn't the loop increment 'i' to 2 only?
@yogeshkushwah9456
@yogeshkushwah9456 Ай бұрын
i am not gonna lie but i did this question in less that 4sec but interviewer set thecondition to 1
@vugarabdurahmanov
@vugarabdurahmanov 7 ай бұрын
Also we can use IIFE and binding
@sahajranipa
@sahajranipa 2 ай бұрын
What if we want to only use var and achieve the same thing without using let or const 🤔🤔🤔
@griffin955
@griffin955 4 ай бұрын
So basically if you are a JS programmer from the last 10 years, you will never run into this problem because you would automatically declare variables with let and never use var.
@GooseGumlizzard
@GooseGumlizzard 3 ай бұрын
lol fr
@sunbertyv3947
@sunbertyv3947 Жыл бұрын
How do you get the different colors, which extensions do you use?
@ConnerArdman
@ConnerArdman Жыл бұрын
You mean the theme? It's called Monokai I think
@1halt-ou1xi
@1halt-ou1xi 5 ай бұрын
Also we could use IIFE function to fix this issue.
@kareemlateefyomi8690
@kareemlateefyomi8690 Жыл бұрын
Learnt new thing today. Thanks
@CDBelfer4
@CDBelfer4 Жыл бұрын
So does let create a copy backed by a function scoped variable? Or initialize the variable on each run? Otherwise how do you keep the state over each iteration?
@ConnerArdman
@ConnerArdman Жыл бұрын
Let is block scoped. In the case of a for loop, it creates a new variable for each iteration, since each iteration is treated as its own block.
@mamourwane4264
@mamourwane4264 Жыл бұрын
Had a similar issue with fade function of jquery, had some divs that were numbered, everytime I removed one of these divs I had to reorder the numbers but because of fade the reorder happened before the deletion and messed up everything.
@AndrewTSq
@AndrewTSq Жыл бұрын
Is this a function that is used in real production?
@ConnerArdman
@ConnerArdman Жыл бұрын
Yes, setTineout is an extremely common function in JavaScript. This specific case almost never comes up though.
@AndrewTSq
@AndrewTSq Жыл бұрын
@@ConnerArdman aha. Ive never used it. Is it used instead of promises?
@ConnerArdman
@ConnerArdman Жыл бұрын
@@AndrewTSq Not really, it's just an entirely different concept. Promises allow you to wait for some asynchronous action to complete. setTimeout sets a timer, so it lets you wait for some predetermined amount of time. These are used a lot for things like animations, polling requests, literal timers, etc.
@jjhassy
@jjhassy 3 ай бұрын
yeah for sure i understood this well 😅
@crazyboyrish2577
@crazyboyrish2577 11 ай бұрын
It will print 333 bcz of var and 012 if we use let
@eliyahusandel9471
@eliyahusandel9471 Жыл бұрын
I had a problem like this when I wanted a few divs to fade in fade out in a specific order and they all just flashed at the same time. I ended up giving it by putting it in a asynchronous function but I still don't understand why it happens and why the synchronous function helped
@eliyahusandel9471
@eliyahusandel9471 Жыл бұрын
And at the end of the function, out of the perenthesis I had to put I,100 Don't know why
@ErikOnABike
@ErikOnABike 8 ай бұрын
This is the same in golang and probably lots of other languages with closure / anonymous functions
@Jackson_Zheng
@Jackson_Zheng Жыл бұрын
Never even knew that var existed. I hardly use let. Mostly const
@GmoneyDaGamer24
@GmoneyDaGamer24 4 ай бұрын
What would const i = 0 do?
@pavankumard5276
@pavankumard5276 Жыл бұрын
I still didn't quite understand why var "i" has only 1 "i" and let has 3 "i". I mean isn't there only 1 memory allocation in the stack for "i" and the same "i" keeps getting updated in both cases?
@ConnerArdman
@ConnerArdman Жыл бұрын
Sort of. The experience the language gives you is essentially what you are describing, but it is not actually the same i every iteration with let. It is a new one being created based on the value of the previous iteration's i. Pasting this explanation from an earlier comment I wrote: This might seem contrary to how the code looks, but this is how the specification is defined, because let is meant to be block scoped. What the JS engine does under the hood for each subsequent iteration when using let is very roughly something like this: 1. Create a new variable environment. 2. Duplicate the iteration variable from the previous iteration's environment into that new environment. 3. Run the increment code on that copy of the iteration variable. 4. Do whatever's inside the for loop. 5. Repeat until the loop condition is false. Here's the extremely difficult the read specification detailing this (10/10 don't recommend actually trying to read it all, but it's interestingly nonetheless): 262.ecma-international.org/6.0/#sec-for-statement-runtime-semantics-labelledevaluation
@pavankumard5276
@pavankumard5276 Жыл бұрын
@@ConnerArdman Thank youuu
@Isaaclolwastaken
@Isaaclolwastaken 3 ай бұрын
Probably the last chill prog video im putting out; like n subscribe
@24pwade
@24pwade Жыл бұрын
Clear as mud
@___m16
@___m16 Жыл бұрын
As an entry level job if I get this I'm working in lidls
@unknownguywholovespizza
@unknownguywholovespizza 11 ай бұрын
Man, that was one of the most confusing thing I've ever seen in JS. Like I didn't know we have a new "i" in each iteration in case of "let". Though, doesn't that make it slower than "var" since it's creating a new "i"?
@yokowasis
@yokowasis Жыл бұрын
What happened before let?
@HorbachenkoVlad
@HorbachenkoVlad Жыл бұрын
can anyone explain the concept of fuction and block scope in this context? "We have a different i for each iteration". Okay, but how doest it gets updated then? On the new iteration, there is a new i and it should be 0 again. I'm sororry if this sounds dumb)
@ConnerArdman
@ConnerArdman Жыл бұрын
var is function scoped. So the var while loop equivalent is roughly this (resulting in only one i ever): function foo() { i = 0; while (whatever) { // do stuff i++; } } let is block scoped. Since loops are blocks, we get a new i for each iteration. But this doesn't restart at 0, it just uses the previous value. This is a bit harder to show with equivalent code, because a lot is happening under the hood to make it work. But generally speaking, it will result in something like this at the end of each iteration: newi = i + 1 delete i i = newi
@HorbachenkoVlad
@HorbachenkoVlad Жыл бұрын
@@ConnerArdman didn't expect that I would get an answer at all) thank you!
@StephenMoreira
@StephenMoreira Ай бұрын
How to know you are about to work in a legacy code base in an interview.
@Sound_.-Safari
@Sound_.-Safari Жыл бұрын
Let vs var lol. Most people always use one or the other, it’d be nice if people used the appropriate choice
@user-fq7jf6cp9p
@user-fq7jf6cp9p Жыл бұрын
Its really just understanding the difference between var and let
@razorred0
@razorred0 9 ай бұрын
Right im confused on how this is notorious. Reading a couple sentences on scope would make it make sense
@mutlugameofhalit
@mutlugameofhalit 4 ай бұрын
@@razorred0 so, could you explain?
@mutlugameofhalit
@mutlugameofhalit 4 ай бұрын
@@razorred0 this is not about scope
@zachb1706
@zachb1706 Жыл бұрын
Hence why you're told to always use let instead of var
@bartek...
@bartek... Жыл бұрын
That's the correct answer!
@magia136
@magia136 3 ай бұрын
Good, c# do this too. seems theres no let in c#
@intptointp
@intptointp 8 ай бұрын
This would be WAY easier to explain with a simple 2-column chart. Variables | value i 3 And so on. Leave out the jargon. Just say... "this one creates only one variable" "this one creates 3"
@LuckLace
@LuckLace Жыл бұрын
when I set the 2nd argument of setTimeOut to 0 it still prints 3 3 3
@ConnerArdman
@ConnerArdman Жыл бұрын
Yeah this is due to the event loop in JavaScript. setTimeout calls an API that essentially just delays the execution of the code by a minimum of whatever delay you set, even if it is 0. However after that delay completes, the code does not immediately run. Instead, it is just queued up in what we call the callback queue (or sometimes called the task/message queue). This is because JavaScript only has a single thread, so the other code that is currently running must complete before the setTimeout callback can be executed. Once all of the other code finishes executing (i.e. the call stack is empty), then whatever code is at the front of the callback queue can finally run. So if you do setTimeout(callback, 0), the callback does not run immediately. It runs after all of the other code completes. In this example, that means it has to wait for the for loop to complete, thus giving the same output of 3 3 3.
@LuckLace
@LuckLace Жыл бұрын
@@ConnerArdman Thank You for the explanation
@PL___
@PL___ Жыл бұрын
How can the settimeout use different i for let i? It makes no sense. It’s the same i variable (reference) but different value because i++.
@ConnerArdman
@ConnerArdman Жыл бұрын
When you use let in a for loop, it is not the same variable every iteration. It is a new one. This might seem contrary to how the code looks, but this is how the specification is defined, because let is meant to be block scoped. What the JS engine does under the hood for each subsequent iteration when using let is very roughly something like this: 1. Create a new variable environment. 2. Duplicate the iteration variable from the previous iteration's environment into that new environment. 3. Run the increment code on that copy of the iteration variable. 4. Do whatever's inside the for loop. 5. Repeat until the loop condition is false. Here's the extremely difficult the read specification detailing this (10/10 don't recommend actually trying to read it all, but it's interestingly nonetheless): 262.ecma-international.org/6.0/#sec-for-statement-runtime-semantics-labelledevaluation
@6.squash.936
@6.squash.936 Жыл бұрын
My man Akshay Saini made me powerful enough to understand it
@philipmrch8326
@philipmrch8326 10 ай бұрын
I saw the issue immediately lmao
@Scott_Stern
@Scott_Stern Жыл бұрын
Before let was invented we had to wrap setTimeOut in an iife
@ShiloBuff
@ShiloBuff Жыл бұрын
I've been programming my whole life and this is the first time I ever seen the pratical difference between "var" and "let". I have never seen a need for "var" anyhow.
@ConnerArdman
@ConnerArdman Жыл бұрын
Yeah there isn't really any need for var anymore. But I think understanding the difference is somewhat important because of how many resources online still use var.
@ShiloBuff
@ShiloBuff Жыл бұрын
@@ConnerArdman True. Good example.
@Rajput_Shubham_
@Rajput_Shubham_ Ай бұрын
Regrets after interview this question was asked me today in my interview and unfortunately not able to answer in case of var
@modularcuriosity
@modularcuriosity 10 ай бұрын
I hate these kinds of questions. They should be asking "This is crap code, here's what I wanted the code to do. How would you write it correctly?"
@seightan
@seightan Жыл бұрын
it should be mentioned to new devs that this is generally not a good practice. `var i = 0` is now a globally available variable and may cause unintended side effects. the general rule: use `const` 98% of the time and use ‘let’ only if you’re mutating data. keep `var` in the trash where it belongs.
@NOBODYxx09
@NOBODYxx09 Жыл бұрын
Man i had to watch many times to get it, i think there is an easier explanation 💀
@TheRealPingu
@TheRealPingu Жыл бұрын
i still have no clue.
@jimg4059
@jimg4059 Ай бұрын
Bro I learned JS like three days ago and I got this right.
@spektree8448
@spektree8448 Жыл бұрын
I didnt know this... i only ever use let before
@thedrew6905
@thedrew6905 Жыл бұрын
now the real question is in which scenario i will ever find myself using this function?
@ehSamurai3483
@ehSamurai3483 4 ай бұрын
I don't get where would I do this in a real world scenario?
@LynXify
@LynXify 4 ай бұрын
I just would not apply to a place still using Javascript when most of the frameworks are trying their level best to be Typescript first
@haraldbackfisch1981
@haraldbackfisch1981 Жыл бұрын
JS is freakin chaotic: const const 2 === const 2 is false but its true for == iirc. Const pointer and all makes sense sure, but its one the most syntactially convoluted things that is so wide spread.. the antithesis to python basically
@martinfernandez4467
@martinfernandez4467 Жыл бұрын
Wht does the 100 do?
@jggabayno
@jggabayno Жыл бұрын
use let and const in up todays development pls
@SoulPrints
@SoulPrints 10 ай бұрын
I interviewed for Angular position and the interviewer asked me only console.log outputs. One of them was this. I am sure he copied it from here 😅
@davitkhoshtaria3336
@davitkhoshtaria3336 Ай бұрын
Why is a var keyword in interviews? Who else uses it?
@ConnerArdman
@ConnerArdman Ай бұрын
It's not about what you will use, it's about demonstrating a deep understanding of the language. You'll also see var pretty frequently in older resources online and in legacy code, so it is still an important concept.
@Lemonator32
@Lemonator32 Жыл бұрын
This only works in "for (of)". In "for (;;)", the initial "let i = 0" is scoped for the entire loop. You need to copy i in a local variable of the iteration, for exanple "let j = i" between the first and second lines, otherwise you get the same problem as using var.
@wisdomeispower
@wisdomeispower Жыл бұрын
Actualy there is no closure. You can solve this question with closure and var.
@joshuawillis1232
@joshuawillis1232 Жыл бұрын
beeep. Wrong. Don't use "let" or "var," use const. Don't use a scoped variable in a closure - setTimeout lets you provide params. All your timeouts are completing at the same time, too, so that seems kinda useless. for (const i of [1,2,3]) setTimeout(x => console.log(x), i*100, i);
@ConnerArdman
@ConnerArdman Жыл бұрын
beeep. Chill. The point of this video isn't to endorse this code as "correct" or even useful. The point is to showcase and explain a commonly asked question intended to test if someone understands the difference between block and function scoping, how closures work, and to some extent how the event loop works. I'd agree with you on never using var, it has pretty much no modern use case (although it is still good to understand as it is used in so many old resources). As for never using let, that's more of a personal preference but there are times where reassignment is just the cleaner solution imo. My general rule, and the rule I've seen applied on every professional team I've worked on, is to default to const but still allow let in those cases where reassignment ends up being the cleaner solution. That said, there are many respected teams/companies/developers basically exclusively using let, and that's fine too. Consistency is more important than anything else.
@berkerdemirer7982
@berkerdemirer7982 Жыл бұрын
Yeah so important to know that in your crud apps ;D
@mattmurphy7030
@mattmurphy7030 4 ай бұрын
This is why c++ capture groups are so superior
@techlifejournal
@techlifejournal Жыл бұрын
Never use var use let
@tuananhle5878
@tuananhle5878 Жыл бұрын
wrap settimeout to iife function and pasa i to param of this function
@DodoLP
@DodoLP Жыл бұрын
and what was the question?
@Iriscgldom
@Iriscgldom Жыл бұрын
This is actually a race condition and on a extremely throttled processor or during certain events or on really slow hardware (although I’m not sure the intersection between a processor that takes 100ms between each iteration and a processor that can actually run JavaScript ) it could output something other like 0 1 2
@ConnerArdman
@ConnerArdman Жыл бұрын
There isn't a race condition. This would be true for similar code in some other languages, but not JavaScript. You would get the same result with a 0ms timeout and a for loop to a million to make it take longer. JavaScript only runs in one thread, so the timeout callback can't run until the call stack is empty (e.g. the for loop completes). All of the timeout callbacks that are ready to run are held in a queue and only get pushed onto the call stack once it is free. As a result, timeouts in JavaScript are minimum amounts of time until code runs, and regardless of hardware the callbacks will never run before the loop completes.
@Iriscgldom
@Iriscgldom Жыл бұрын
@@ConnerArdman ahhh that makes complete sense thank you for the quick reply Conner
@zeez7777
@zeez7777 Жыл бұрын
race condition in a single threaded language, lets go
@Iriscgldom
@Iriscgldom Жыл бұрын
@@zeez7777 race conditions happen all the time in single threaded languages, its called asynchrony... and setTimeout is a classic culprit
@zeez7777
@zeez7777 Жыл бұрын
@@Iriscgldom What you're talking about are not real race conditions. Race conditions can only happen if you have atleast 2 threads. Event loop screw ups are similar but not race conditions.
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 2,9 МЛН
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 95 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 15 МЛН
Beginner React.js Coding Interview (ft. Clément Mihailescu)
36:31
Ben Awad
Рет қаралды 2,2 МЛН
20 Programming Projects That Will Make You A God At Coding
14:27
The Coding Sloth
Рет қаралды 1,4 МЛН
The World Depends on 60-Year-Old Code No One Knows Anymore
9:30
Coding with Dee
Рет қаралды 976 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 283 М.
Must Know Javascript Interview Questions
6:10
Catherine Li
Рет қаралды 113 М.
7 Years of Software Engineering Advice in 18 Minutes
18:32
React Interview Questions | Beginner to Advanced
26:42
PedroTech
Рет қаралды 41 М.
5 JavaScript Concepts You HAVE TO KNOW
9:38
James Q Quick
Рет қаралды 1,4 МЛН
2.5 Years Experienced Best JavaScript Interview
2:03:06
Anurag Singh ProCodrr
Рет қаралды 310 М.
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 2,9 МЛН