JavaScript Event Loop: How it Works and Why it Matters in 5 Minutes

Поділитися
Вставка
  • Опубліковано 15 чер 2024
  • One of the most important concepts to learn in JavaScript! The Event Loop is core to how JavaScript handles asynchronous code. In this video, we'll break down the call stack, event loop, and event queue to show you how JavaScript works under the hood.
    *Advent of CSS and JavaScript Courses are 50% off with code BLACKFRY through 12/5/22*
    www.adventofjs.com/
    www.adventofcss.com/
    ** Resources **
    Original Article - dev.to/nodedoctors/an-animate...
    Follow Andrew on Twitter - / andrewhu368
    *QUESTIONS ABOUT MY SETUP*
    Check out my Uses page for my VS Code setup, what recording equipment I use, etc. www.jamesqquick.com/uses
    *STAY IN TOUCH 👋*
    Check out the Podcast - compressed.fm/
    Courses - jamesqquick.com/courses
    Newsletter 🗞 - www.jamesqquick.com/newsletter
    Follow me on Twitter 🐦 - / jamesqquick
    *DISCORD*
    Join the Learn Build Teach Discord Server 💬 - / discord
    ** Timestamps **
    00:00 - Intro
    00:50 - Advent of CSS and JavaScript Course Discounts
    01:18 - JavaScript is Single Threaded
    02:09 - JavaScript Call Stack
    04:15 - Event Queue and Event Loop
    06:20 - Wrap Up

КОМЕНТАРІ • 156

  • @Mo-Sam
    @Mo-Sam Рік тому +21

    I loved it, thank you. It would be so great if you make a series like this , explaining advanced concepts under 5 minutes.

  • @thanhattran284
    @thanhattran284 Рік тому +32

    Finally, after looking for n^1000 videos, forums... I found a person who can explain these terms in real simple and understandable way 😭really appreciate your work man.

    • @rafadydkiemmacha7543
      @rafadydkiemmacha7543 15 днів тому

      Really? It was a very lame explanation. He skipped many essential parts while dragging on single things.

  • @georgenonis5967
    @georgenonis5967 4 місяці тому +1

    Amazing video. Whenever I forget how it works I just jump over here and everything is clear again. Thank you mate!

  • @rick.d
    @rick.d Рік тому +30

    Understanding the LIFO/FIFO nature of the call stack vs the event stack is really cool, thank you.

    • @JamesQQuick
      @JamesQQuick  Рік тому

      So glad that was helpful :)

    • @goyii
      @goyii 3 місяці тому +3

      This is true but i think its important to not call event queue "stack" when the real data structure its a Queue. When you think about stacks and queues from real life everything makes sense

  • @felixurrutia4246
    @felixurrutia4246 Рік тому +1

    Excellent explanation. This is exactly what I was looking for.

  • @kostiantynkarzhanov9216
    @kostiantynkarzhanov9216 8 місяців тому

    Thank you! Very concise and simple explanation of not an easy concept! 💛

  • @praveenvarma1
    @praveenvarma1 Рік тому +1

    Super bro ... u explained a complex theory so simply with a beautiful animation.

  • @user-dc4qs5vs5z
    @user-dc4qs5vs5z Місяць тому

    Really nice and simple way of explaining complex concepts. Great video

  • @denniszenanywhere
    @denniszenanywhere Рік тому

    I like this overexplanation. Please do more. I am even watching tiktok videos that loop short coding lessons and they’re sinking in more.

  • @smachohalla
    @smachohalla Рік тому

    Thanks Mr. Quick. This was neat magnificently explained

  • @ahlembs790
    @ahlembs790 6 місяців тому +1

    i am really thankful for this video just before my interview, , feel having clear idea about what happen behind the scenes with node.js, please more advanced topics like this with such great explanantion

    • @JamesQQuick
      @JamesQQuick  6 місяців тому

      Wow, that's so cool to hear. Hope it went well!

  • @umangkathuria7097
    @umangkathuria7097 3 місяці тому

    Always come here for a quick refresher before I have to explain it to anyone :) That article is excellent in itself too! great content!

  • @elielberra2867
    @elielberra2867 Рік тому

    Amazing explanation, it was very clear! Thanks :)

  • @indraappkey
    @indraappkey Рік тому +3

    one of the clearest explanation of event loop, thanks James

  • @abdullah.nayem.enosisbd
    @abdullah.nayem.enosisbd Місяць тому

    You just made it crystal clear. Thanks so much.

  • @damianodamian7907
    @damianodamian7907 Рік тому

    Now I got it, thank you. That is a really great explanation James! You've got a new sub

  • @dpetrik99
    @dpetrik99 Рік тому

    Great and short tutorial! Highly recommended

  • @kasiad7291
    @kasiad7291 Рік тому +1

    Perfect explanation, thank you!

  • @user-ol6tq5hw7s
    @user-ol6tq5hw7s Рік тому

    Great video, keep going James! :)

  • @jean-pierrebanchereau8474
    @jean-pierrebanchereau8474 Рік тому

    Perfectly explained. Thank you

  • @koffworld7857
    @koffworld7857 Рік тому

    Very clear explanation... great video!

  • @youngshehata2348
    @youngshehata2348 Місяць тому

    That was amazing explaining, thank you so much

  • @Yunraii
    @Yunraii 11 місяців тому

    Thank you very much. It was explained very well and I finally understood what an JS Even Loop is. You did a great job in explaining it!

  • @godswillumukoro8908
    @godswillumukoro8908 Рік тому

    Great explanation here James!!!

  • @Walocial
    @Walocial 4 місяці тому +1

    Awesome explanation, much appreciated. Thanks :)

  • @rayenselmen603
    @rayenselmen603 8 місяців тому

    Finally ! Amazing explanation Sir !

  • @Rashomon69
    @Rashomon69 9 місяців тому

    Brilliant. I think I finally understand.
    SUBBED!

  • @manerVlog
    @manerVlog 3 місяці тому

    Awsome sir now I understand watching many videos this is very simple to understand.

  • @alexmiserandino1062
    @alexmiserandino1062 5 місяців тому

    fantastic video, thanks!

  • @utkarshsinghalus3085
    @utkarshsinghalus3085 Рік тому

    wow great video , it simple thanks for the virtual presentation

  • @johnshepard3197
    @johnshepard3197 8 місяців тому

    Thanks for the quick refresher, man! Plus correcting my pronunciation of "lib-you-vee" library ;)

  • @ming3957
    @ming3957 Рік тому +22

    Expected a 5 minute video but I got 2 whole extra minutes of James for free! What a deal

  • @DavyJones124
    @DavyJones124 Рік тому

    this explanation was amazing

  • @RihaLV
    @RihaLV Рік тому

    Great video, thank you!

  • @eduardocuadraneyra
    @eduardocuadraneyra 2 місяці тому

    THNKAS A LOT i was havnig a hard time to get this idea but you have explained really simple, so Here you have a new student, thanks again and have a great day...and if your a reading this and your not the teacher , have a great day to smile, and remember you are important to.

  • @kacygilbert9434
    @kacygilbert9434 11 місяців тому

    Thanks for this!!!

  • @hendodesktasks
    @hendodesktasks Рік тому

    Great Explanation, Thanks a lot

  • @EmmanuelIbikunle
    @EmmanuelIbikunle Рік тому

    Very good explanation. Thanks

  • @user-wr4yl7tx3w
    @user-wr4yl7tx3w 11 місяців тому

    really well explained.

  • @ctchen1988
    @ctchen1988 3 місяці тому

    awesome...thank you so much.

  • @afridperingaden846
    @afridperingaden846 6 місяців тому

    very good explanation

  • @dorels6205
    @dorels6205 11 місяців тому

    Wow, really, really understandable!!!

  • @asrnyagmur9802
    @asrnyagmur9802 Рік тому

    Clearly understand ❤ followed

  • @Yan-vi6fd
    @Yan-vi6fd Рік тому

    Amazing explanation!!

  • @abdelrahman1669
    @abdelrahman1669 Рік тому +1

    really impressive thanks man

  • @TheZeiZei
    @TheZeiZei Рік тому

    Great explanation!

  • @diegogutierrez7032
    @diegogutierrez7032 Рік тому

    Something to add there is that. This is the event loop explained from the NodeJS side. If you take a look at the event loop in the browser side. It's much mure complex due to UI taks, Render Tasks, Dom Tasks, Net tasks.

    • @JamesQQuick
      @JamesQQuick  Рік тому +1

      I’m not sure that’s true? How is it different? It still uses the same offloading mechanism for async work right?

  • @ricardocnn
    @ricardocnn Рік тому +1

    Awesome tips!

  • @liamdev5145
    @liamdev5145 Рік тому

    Woa truly appreciate it

  • @marcelukeje3510
    @marcelukeje3510 2 місяці тому

    I'm here with no background knowledge on JavaScript. I need to know this for an assignment and I find the video insanely easy to understand

  • @ghaithjr.9189
    @ghaithjr.9189 Рік тому

    note : libuv is written primarily in c ,
    great explanation king

  • @shuvra03
    @shuvra03 9 місяців тому

    Best video on node js event loop in 5 minutes

  • @dilshodturabov
    @dilshodturabov Рік тому

    thank you pls do not stop thank you

  • @SonAyoD
    @SonAyoD Рік тому

    Wow, I finally get it. Where does the heap come into this?

  • @juliorodriguez4946
    @juliorodriguez4946 10 місяців тому

    Thank you so much...

  • @scottweedo6707
    @scottweedo6707 10 місяців тому

    Thanks 🙌🏼

  • @diwakardayal954
    @diwakardayal954 Місяць тому

    Cleannnn thanks

  • @noorulamin2652
    @noorulamin2652 Рік тому +1

    Awesome! I have few questions 1. few days ago i was reading a medium article in which they use the term callback queue but here you mentioned event queue. 2 they called it Node APIs and you mentioned Libuv APIs. 3. They also did mention what's called MicroTask Queue, but here it was missing. 4. Does Event Loop handles only Async operations? 5. How does Event Loop handles setTimeout functions?

    • @JamesQQuick
      @JamesQQuick  Рік тому +11

      So I believe "callback queue" would be the same as the "event queue". I would assume Node APIs are also referring to the Libuv APis as well. I have never heard of the MicroTask Queue, so I'm not sure where that fits in. For the event loop, yes, it is only looking for async operations that have finished running.
      setTimeout takes a callback function. So, when setTimeout is called, it sends it off to the underlying APIs to be handled. When the timeout is done, it then throws the event/callback onto the Event Queue. The Event Loop will find the event on the queue and pass it back to JavaScript call stack.
      hope that helps!

    • @ivansandoval3757
      @ivansandoval3757 Рік тому +1

      Same doubt as yours, I just watched a different video where the event loop components are mentioned as follows: Call stack, event loop, micro tasks queue, macro tasks queue and scheduled tasks queue.

    • @And1997Ruz
      @And1997Ruz Рік тому +2

      @@ivansandoval3757 You have probably watched the explanation of the browser event loop, whereas here it's the nodejs event loop
      They work in a different way. Not to mention the fact that browsers don't have the Libuv API

  • @mohmmadjesus6710
    @mohmmadjesus6710 Рік тому

    Wonderful Info Sir! please make a series on vue js

    • @JamesQQuick
      @JamesQQuick  Рік тому +1

      Glad you enjoyed it! I still haven’t really spent time with vue yet but I want to!

  • @Kanal5909
    @Kanal5909 Рік тому +3

    For me the most tricky part is that you never really know when the function you call is going to be asynchronous or synchronous. For example if you read data from a file and then process it, for small files it will be synchronous and for the files with let's say 300k lines it will most likely be asynchronous (it also depends on what you do with these lines). It's obvious that all HTTP requests are asynchronous so this is quite easy to handle or predictable at least.

    • @JamesQQuick
      @JamesQQuick  Рік тому +6

      Well, I think you can tell something is asynchronous based on two things:
      1. it returns a promise
      or
      2. if takes a callback function as a parameter

  • @prinzadamz
    @prinzadamz 4 місяці тому

    Thank you 👍👌

  • @maxlev621
    @maxlev621 Рік тому

    Best explanation ever

  • @iiinicky6224
    @iiinicky6224 Рік тому

    Thanks!

  • @satorugojo6
    @satorugojo6 Рік тому

    thanks a lot man

  • @reeq5748
    @reeq5748 9 місяців тому

    U saved me man. Thank you.

  • @harishraj3616
    @harishraj3616 Рік тому

    Clearly understand ❤️

  • @misterl8129
    @misterl8129 2 місяці тому

    Thanks for the video! And yes, I have a question.
    So we know that every async procedure will go to the event queue. And it will only passed to the call stack if this is empty.
    So...
    1. What about if we have a large large project, with many sync procedures, the event loop will wait until ALL of them finish before pass it to the call stack and give the result?
    2. How it work then the "await", if we know that we need to clear the call stack before pass the async functionality. How it work when we need to have some data before proceed with the other instructions?
    For example
    data mutation 1,
    data mutation 2,
    data mutation 3,
    await for something()
    data mutation 4
    How it works when we need to have the "something" before go to the data mutation 4
    Thank you!

  • @arjinpella6071
    @arjinpella6071 4 місяці тому

    thanks bro

  • @alexanderwu
    @alexanderwu 3 місяці тому

    Great video. One complaint, this is the node event loop which isn't exactly the same thing as JavaScript

  • @neeravvinod2774
    @neeravvinod2774 5 місяців тому

    great video

  • @doniaelfouly4142
    @doniaelfouly4142 2 місяці тому

    Thanks

  • @doniaelfouly4142
    @doniaelfouly4142 Рік тому

    thanks

  • @mikkun_
    @mikkun_ 5 місяців тому

    so if we have async/await, it will put the asynchronous to libuv, then event queue, and stack it to the call stack along with the other synchronous tasks?

  • @krateskim4169
    @krateskim4169 Рік тому

    great explanation

    • @JamesQQuick
      @JamesQQuick  Рік тому

      Thank you! Did you already have a good idea of what the event loop is?

    • @krateskim4169
      @krateskim4169 Рік тому

      @@JamesQQuick nope not until today, learnt something new

    • @JamesQQuick
      @JamesQQuick  Рік тому +1

      @@krateskim4169 Ah so glad it helped!!

  • @LeLeTesoro
    @LeLeTesoro Рік тому

    Hi there, I'm watching this in April - if I sign up on the website you linked, can I still see the 24 JS and CSS challenges you did for Advent?

  • @user-rdr1712
    @user-rdr1712 4 місяці тому

    Hi, 5:15 Eventloop use to take the event from queue and pushes into the callstack only when the callstack is empty. Means, eventloop do not take the event from the queue when there is an event(atleast 1) in the callstack. Am I right?
    What if there are multiple events waiting in event queue. Do the eventloop dequeues all the events one by one from eventqueue when the call stack is empty or it will dequeue only one event?

  • @hassanad94
    @hassanad94 5 місяців тому

    What about js workers? You can do multi threaded tasks with it.

  • @Pareshbpatel
    @Pareshbpatel 2 місяці тому

    A nice walkthrough the JS Event Loop. Thanks, James
    {2024-04-15}

  • @andrewwall2730
    @andrewwall2730 Місяць тому

    Just a minor correction, libuv is written in C, not C++

  • @mahedihassanshawon4821
    @mahedihassanshawon4821 Рік тому

    awesome

  • @ofeyofey
    @ofeyofey Рік тому

    He says the event loop is "first in last out" is that out from the place it came it in or other end?

  • @ehtisham_hashmi
    @ehtisham_hashmi Рік тому

    where I can find more videos on js concepts?

  • @krnkoravi
    @krnkoravi 11 місяців тому

    First time hearing about LIBUV API. I thought async task directly goes from callstack to event queue. LIBUV only applicable in nodejs? Still not clear what exactly processing LIBUV does to async task before it goes to event queue

  • @anushree3744
    @anushree3744 5 місяців тому

    what does it mean when we say that event loop is blocked and how does that happen?

  • @devonchia5976
    @devonchia5976 Рік тому +1

    so if there is an incoming synchronous code and a callback at the same given time, what is the precedence for the call stack?

    • @AahadCyborg
      @AahadCyborg Рік тому

      when synchronous code and a callback are both triggered at the same time, the synchronous code takes precedence over the callback function in the call stack. This means that the synchronous code will be executed first, and then the callback function will be added to the call stack and executed once the synchronous code has finished running.

  • @nikman5541
    @nikman5541 Місяць тому

    Okay this is Node platform with C++ wrapper. but what about browser where we dont have Node and still have async JS?

  • @moneyfr
    @moneyfr Рік тому +1

    Getter and setter please and why using it

    • @JamesQQuick
      @JamesQQuick  Рік тому

      You're interested in why people use getters and setters?

  • @ghostkee5031
    @ghostkee5031 Рік тому

    you gonna confuse people between the built in javascript event loop and nodejs thread pool + event loop which libuv provide

  • @rahibrustemzade407
    @rahibrustemzade407 Місяць тому

    what is microTask and macroTask ?

  • @mkrzyzowski
    @mkrzyzowski 9 місяців тому

    What when call stack is empty and event queue too and some async task is running in libuv thread. How JS know to wait for libuv?

    • @JamesQQuick
      @JamesQQuick  9 місяців тому

      Hmm, well JavaScript land just knows to constantly check the event queue when it's done running code. So as it constantly checks, eventually libuv will put something back on the queue for JavaScript to process.

  • @_naemi
    @_naemi 5 місяців тому

    So this mean that "console.log()" doesnt go to event loop?

  • @SiteMentor
    @SiteMentor Рік тому

    nice

  • @user-ou9ky2cj4e
    @user-ou9ky2cj4e Рік тому +1

    For 5 mins it is fine, but the micro task loop concept is missing

  • @shashwotbhattarai8731
    @shashwotbhattarai8731 8 місяців тому

    i don understand the part where the stack is empty and event loop pushes the callback to the stack. will the stack ever be empty? when does the stackbecome empty?

    • @goyii
      @goyii 3 місяці тому

      The stack gets filled once javascript code its readed. Then it will execute functions from the call stack starting from the top, cleaning out functions once executed. In this particular case. The only function added to the call stack was a console.log. But the code also executed an asynchronous function. Wich is sent to the Lubuv Api to ne handled. Once this operation is completed is sent to the event queue. The event loop checks if the call stack is empty and if there is something inside the event queue. If that happens, it will move this callback from the quewe to the call stack. Execute the callback and clean the stack again.

  • @adhithyankrishna8220
    @adhithyankrishna8220 5 місяців тому

    It kind a faking multi thread libuv api is like giving task ti subsystem to excute like os or kernal to excute the fetch operating libuv is waiting for result when it got its result it send to evet quew which means node is not creating thread but it use sub system thread which is also running on same cpu means is multithreaded

  • @notarealhandle123
    @notarealhandle123 2 місяці тому +1

    I've been developing in JavaScript for over 20 years now. But when I was interviewed recently for a new job, and asked about JavaScript event loop, I could not remember it for the life of me. This is mainly academical knowledge, not something that you need as an experienced developer. People who do interview are often dumb, and do not understand that.

  • @TheBlackmanIsGod
    @TheBlackmanIsGod 8 місяців тому

    Explain the “popping off the top” more

  • @conallen3166
    @conallen3166 3 місяці тому

    🔥

  • @nikolozmacharashvili1212
    @nikolozmacharashvili1212 6 місяців тому

    hey james i have a question. i have a recruiter promising me training and job placement. is this for real or is there a catch. i asked about info, they said when u get to the country than will talk, but keep on liking my projects on linkedin

    • @nikolozmacharashvili1212
      @nikolozmacharashvili1212 6 місяців тому

      also when u said, if the callback is an async code, the cycle repepats. officialy some async code went into the LIBUV API, now was that the callback or some different code ?

  • @DavidLevy-qg7hd
    @DavidLevy-qg7hd Місяць тому

    nice video but the title is a bit misleading, this is actually the node.js event loop as you say at the end of the video, not the javascript event loop as the title says.. JS natively does not use libuv, instead it uses the browser's built-in Web APIs to handle asynchronous tasks