What is an IIFE? | Javascript IIFE Function Expressions Explained

Sdílet
Vložit
  • čas přidán 25. 08. 2024

Komentáře • 72

  • @ichiroutakashima4503
    @ichiroutakashima4503 Před rokem +5

    This channel needs more subscribers. The info is easy to digest. And your description and links you give are the holy grail needed by every beginner to learn.

  • @srinivasaraoyp3640
    @srinivasaraoyp3640 Před 2 lety +1

    I am trying to learn IIFE, this video demo is simply the best. In the institutions, they only show one example. Here are so many examples. Thank you.

  • @nikitak9628
    @nikitak9628 Před 2 lety +2

    Dave, this is a fantastic explanation of IFFEs. Very thorough, especially for going through the module and the revealing module pattern.

  • @ranjankumar1324
    @ranjankumar1324 Před 3 lety +1

    Congratulations on 4k.., much more to come

  • @ibrahimyoussef4489
    @ibrahimyoussef4489 Před rokem

    I search about thing and open any video , then I sea Dave video , immediately close the video and open the Dave's video , YOU ARE AWESOME

  • @CodeWithTomi
    @CodeWithTomi Před 3 lety +2

    Nice video... And congrats on 4k

  • @hotkatgotama
    @hotkatgotama Před 2 lety +1

    Dave, I just recently started watching your tutorials - superb content. Currently, I am doing a deep dive into ReactJs, NextJS and NodeJS (from a .NET background). I would love to see (in addition to your usual generic-type examples) some (at least one) real world examples, this might make your videos a bit longer - but well worth the effort. For example, these variations of the Module Pattern - I can see that a good use case for them would be when you only need one instance of an object - without inheritance, singleton code and all the other baggage. Would be great to see your response. Fantastic channel.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety

      Thank you, Katim! And thank you for the request / suggestions. 🙏 One instance of an object would be a singleton pattern if I am understanding your request correctly? Design patterns would be a great series to tackle! 🚀

  • @aman7555
    @aman7555 Před 3 lety +1

    Hello Dave, wonderful tutorial as always. A quick input, in the increment counter example, we don't need to pass counter to credits as it already has access to it because its in the lexical scope, [I understand scopes and closures better thanks to you]
    const increment = (()=>{
    let counter=0;
    console.log(counter);
    const credits = () => console.log(`I have ${counter} credits`);
    return () => {counter++; credits();}
    })();

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +1

      That is a good observation about lexical scope. If I didn't use the num parameter placeholder, it could have been written that way. 💯😀

    • @shobhitgarg6897
      @shobhitgarg6897 Před rokem

      @@DaveGrayTeachesCode Hey , Great Tutorial !! Just one doubt, I have checked with normal functions they also have access to counter variable. So like whats the difference?
      const increment = ()=>{
      let counter = 0;
      console.log(counter);
      const credit = (num)=>console.log(`I have ${num} credits`);
      return ()=>{counter++; credit(counter)}
      }
      const incrementFunction = increment();
      console.log(incrementFunction);
      incrementFunction()
      incrementFunction()
      incrementFunction()

  • @B-Billy
    @B-Billy Před 2 lety +1

    So much content in such a less time.. Bravo and subscribed !!

  • @user-qg5ow7of4e
    @user-qg5ow7of4e Před 3 měsíci

    Hi Dave, you are doing an amazing job, can you come up with more javascript advance concepts

  • @craigburton4447
    @craigburton4447 Před 2 lety

    Such a good tutorial Dave, several 'aha' moments for in it. Thanks!

  • @Miguel-Barcelo
    @Miguel-Barcelo Před 2 lety

    Hi Dave, thanks for your service, you have my like on every video that I watch, really appreciate it

  • @shawnjohnson3433
    @shawnjohnson3433 Před rokem

    Very good teacher!

  • @Panjax
    @Panjax Před 4 měsíci

    I'd have liked an explanation why these variations exist. Like the module pattern vs the revealing module pattern. I can see they're different but why? what problem does it solve being different like that. And therefore, why would we use it?

  • @shineLouisShine
    @shineLouisShine Před 2 lety

    Fascinating concept! Although it does feels as if it requires a lot of practicing to be truly familiar with and apply it while coding with in high skillability.
    Thanks..

  • @delyanmitev7728
    @delyanmitev7728 Před rokem

    Thanks my dude. 1st result when I googled and I never never got to the 2nd one.

  • @gordonmullen7272
    @gordonmullen7272 Před 3 lety

    Thanks again for a job well down: I see things I didn't before so THANK YOU !! hungry for more Mr. Grey, you're a Master Code Chef who plays a mean Van Halen lick. I used to be a Keyboard player in a band. Used an old Korg 800 + a whole rack. telling my age now lol. That was back when jump just hit the radio and I loved it.... anyway thanks so much!!!

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety

      Thanks Gordon! 🙏 80s rock does have great guitar and keys! 🤘🤘💯

  • @jingli9232
    @jingli9232 Před rokem

    thx for sharing you knowledge

  • @emreozgun3846
    @emreozgun3846 Před 3 lety

    Informative as always Dave!

  • @ua2894
    @ua2894 Před 6 měsíci

    What would be the use and benefit of injecting a namespace object?

  • @ahmad-murery
    @ahmad-murery Před 3 lety

    Injecting name space is useful to plugin more functionalities to the App as needed,
    for example we can create another separate IIFE to provides a decrement method to expand what the App can do
    Thanks Dave,

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +1

      Absolutely Ahmad. Glad to see your comment here! 🚀💯

    • @ahmad-murery
      @ahmad-murery Před 3 lety

      @@DaveGrayTeachesCode Finally 😁, I thought I'll never be able to comment on your videos again,
      Thanks Dave and I appreciate your help

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +1

      @@ahmad-murery agreed! For the context of anyone else reading this, we could not figure out why YT prevented his comments on my last video. Ahmad provides good comments, and I do not block them. Glad to have you back 🙏

    • @ahmad-murery
      @ahmad-murery Před 3 lety

      @@DaveGrayTeachesCode So true, maybe YT has marked my comments as spam and auto deleted them because I always start them with "Hello Dave", so I didn't do it this time 😎

  • @darkhorsemusicco.5156
    @darkhorsemusicco.5156 Před 2 lety

    Excellent excellent video. Thank you

  • @dumchik7290
    @dumchik7290 Před 11 měsíci

    > function fun1(){return "hello"}()
    Uncaught SyntaxError: Unexpected token ')'
    > function fun2(x){return x*x}(5)
    5
    > 0,function fun3(x){return x*x}(5)
    25
    As expected in first case I get error as I didn't use paranthesis. But how the second case and third case get executed?

  • @chetanjain4616
    @chetanjain4616 Před 2 lety

    Love the series, I hoped there will be one for bind, call as well

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

      I have not made those.. yet. Great requests! Thank you 🙏

    • @chetanjain4616
      @chetanjain4616 Před 2 lety

      @@DaveGrayTeachesCode I think video on those topics would complete the advance javascript series

  • @dzienisz
    @dzienisz Před 10 měsíci

    Where would you use IIFE in real case application? Any particular module?

  • @tularamsa5814
    @tularamsa5814 Před 2 lety

    awesome!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • @yabuking84
    @yabuking84 Před 3 měsíci

    isnt it only VAR can be global variables?

  • @jiweihe3413
    @jiweihe3413 Před 2 lety

    A bit confused about the last example "injecting a namespace object" at the end of the video. Why the input to the function is not "namespace=XXX" but "window.App=window.App" instead?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 2 lety +1

      In the example, the window object is being passed in and we are creating a namespace in the property App. At 13:50 I explain that it much like defining const App = { } ...those are the arguments passed in when the function is called. In the example, the word "namespace" is a parameter placeholder in the function definition.

    • @jiweihe3413
      @jiweihe3413 Před 2 lety

      @@DaveGrayTeachesCode thank you. Just realized namespace is not a random object name.

  • @st10689
    @st10689 Před rokem

    Hi my question is why it is necessary to wrap the whole function definition inside parenthesis and then call it.
    I mean why this does not work const test = ( ) => { console.log(10) } ( );
    Why it needs the added parenthesis, so basically be like that to work const test = (( ) => { console.log(10) }) ( );
    OR THIS
    ( ) => {console.log("Hello") ( ); -> Not working
    (( ) => {console.log("Hello")) ( ); -> Working

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      Good question! It needs to be grouped together so the operators () are calling the complete function. For another example, if we have:
      'Dave'+'Gray'.toUpperCase() ...what do you think will be uppercase?
      Only Gray because toUpperCase() is only called on the last string. However:
      ('Dave'+'Gray').toUpperCase() makes both Dave and Gray uppercase.

  • @Really_riel
    @Really_riel Před rokem

    When I entered the code at 3:03 . I keep getting a reference error saying “nun is not defined”.
    Please I’m confused.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      There is a correction that pops-up at 1:30 and is noted in the description. That should help!

  • @jiweihe3413
    @jiweihe3413 Před 2 lety

    10:30 module pattern looks a bit like factory function but is immediately invoked. Is there any relation between the two?

  • @shanemarchan658
    @shanemarchan658 Před 3 lety +1

    and that's why bananas have no toes..
    if u need a tutorial to that, ive got a link in the description down below

  • @user-jw5op8sl6n
    @user-jw5op8sl6n Před rokem

    myIffy() function has no parameters. But while invoking a parameter is used (num =0). How is it? Could you please explain it?

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      Sounds like a default parameter: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters

    • @user-jw5op8sl6n
      @user-jw5op8sl6n Před rokem

      @@DaveGrayTeachesCode , thanks for your reply. I'm learning a lot from your videos.

  • @ravivarma7837
    @ravivarma7837 Před rokem

    every function create its own scope so the variables or functions inside the function would only be created in the same function scope and it would not populate global scope
    i am very confuse as the below code does the same as IIFE
    const x = "whatever global Scope";
    const helloworld = () => "Hello World from global Scope";
    function foo(){
    const x = "whatever foo Scope";
    const helloworld = () => "Hello World from Foo Scope";
    console.log(x);
    console.log(helloworld());
    }
    foo();
    console.log(x);
    console.log(helloworld());

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před rokem

      In your example, you have 3 globals: x, helloworld, and foo. You could wrap all of those in an IIFE and have no globals. You are correct that each function creates its own scope. This is also true of an IIFE function. All of the other functions you create would be within the scope of the IIFE and not in the global scope.

    • @ravivarma7837
      @ravivarma7837 Před rokem

      after months of wrapping my head around finally I understood thanks alot ​ @Dave

    • @abdulrehman-rt5mz
      @abdulrehman-rt5mz Před rokem

      @@ravivarma7837 same confusion going with me, can u pls what did u conclude ?

  • @DaveGrayTeachesCode
    @DaveGrayTeachesCode  Před 3 lety +3

    IIFEs look unusual when you first see them. No worries! In this tutorial I break down what an IIFE is and how an IIFE is used in Javascript. This tutorial also references several other Javascript concepts including recursion, ternary statements, scope, and closure. I have links to tutorials about all of those concepts in the description of the video. If you are just starting to learn Javascript, I suggest starting with my full 8 hour Javascript tutorial found here: czcams.com/video/EfAl9bwzVZk/video.html Note: There is a default parameter placement correction of the example at 1:30 noted in the description. Doh! Needed more ☕☕☕

  • @jcubic
    @jcubic Před 3 lety

    I'm afraid you have a logic error in your code })(num = 0); will create a global variable because it's not a parameter just an expression. You want to put this in function argument not when you call it. I would add a banner to correct that.
    Also you're using IFFE it isolate the scope but with const and let you're using you can just curly braces you don't need IFFE anymore in ES6 and beyond.

    • @DaveGrayTeachesCode
      @DaveGrayTeachesCode  Před 3 lety +1

      Thanks for the note. 🙏 I did not consider the default parameter placement correctly when the IIFE is called. Note will be added. Update: It seems YT no longer allows video annotations, but I have added a note in the description.
      Concerning your 2nd comment on not needing IIFE after ES6 and beyond, I state in the video this is intended to identify what you may see in legacy code. Legacy code will likely use var, but I can't make myself go back lol. I agree that curly braces create a local block scope and you can verify in my scope tutorial: czcams.com/video/_E96W6ivHng/video.html Thanks again for the comment! 😀