• Wednesday , 3 June 2020

Function Declarations VS Function Expressions in JavaScript

Code Canyon



JavaScript provides 2 methods for defining a function: the function declaration and the function expression. In this video we examine the difference, which includes hoisting and the emphasis on first class functions. We also take a look at when you might want to use a named function expression as opposed to an anonymous function expression.

Would you like to help keep this channel going?
https://www.patreon.com/allthingsjavascript

For a complete list of all our tutorials:
http://www.allthingsjavascript.com/youtube.html

Take the Learn Modern JavaScript: Getting Started or the Learn Modern JavaScript: Advanced Topics courses at a huge discount:
https://www.udemy.com/learn-modern-javascript-getting-started/?couponCode=YOUTUBE
https://www.udemy.com/learn-modern-javascript-advanced-topics/?couponCode=YOUTUBE

Original source

3d Ocean

Related Posts

25 Comments

  1. pavan kumar
    April 19, 2019 at 17:01

    Thank you Sir.

  2. Ricardo Belmont
    April 19, 2019 at 17:01

    I would give you a million likes if I could

  3. Bruce Lee
    April 19, 2019 at 17:01

    I still dont get why would I prefer one over another

  4. Rafael Santos
    April 19, 2019 at 17:01

    You said when we want use an IIFE should use expression function, but i tried with functions declarations and did works. Samething in functions by parameters

  5. Jackie Santana
    April 19, 2019 at 17:01

    who else is completely lost?

  6. exikoz
    April 19, 2019 at 17:01

    Dude thank you for saving me from this headache i got for trying to understand the difference between these two.

  7. Jon Snow
    April 19, 2019 at 17:01

    Please make a tutorial on react!

  8. Jon Snow
    April 19, 2019 at 17:01

    Make this channel famous!
    Don't stop making videos!

  9. Brett Walker
    April 19, 2019 at 17:01

    Solid explanation. The audio was good enough for me!!

  10. Ahmed Ben Ammar
    April 19, 2019 at 17:01

    much appreciation goes forever

  11. ANKIT TRIPATHI
    April 19, 2019 at 17:01

    There was some issue with the audio, but regardless great explanation as always

  12. Vorname Nachname
    April 19, 2019 at 17:01

    Check out Atom Editor.

  13. Andrew Lim
    April 19, 2019 at 17:01

    for the first time I understand hoisting. thanks

  14. Paul Cass
    April 19, 2019 at 17:01

    I'm trying to get my head around why it's best to use a function expression in the second example. What is the difference of using a declaration instead?

    function runIt(a) {
    console.log(a);
    a();
    }

    runIt(function() {
    var b = " Now";
    console.log("Running" + b);
    });

    Many thanks!

  15. Woman InTech
    April 19, 2019 at 17:01

    Great examples. Thanks!

  16. Harry Manchanda
    April 19, 2019 at 17:01

    Also in ES6,

    “`
    console.log(awesomething);
    awesomething();

    const awesomething = () => {
    console.log('Express awesomething');
    }
    “`

    This will return `awesomething is not defined`
    instead of

    “`
    log => undefined
    function => `Uncaught TypeError: something is not a function`

  17. Aniket Tiwari
    April 19, 2019 at 17:01

    Just to make one point correct as you said at 2.46 "Code is parsed and elevated to the top of the page". But you have misunderstood hoisted thing. What does happen is that variable and function declarations are put into memory during the compile phase, but stays exactly where you typed it in your coding. Refer this link it will give you better understanding https://developer.mozilla.org/en-US/docs/Glossary/Hoisting

  18. Велизар Китанов
    April 19, 2019 at 17:01

    Very good video, but next time check audio quality. 🙂

  19. David Hahn
    April 19, 2019 at 17:01

    closures and callbacks would be helpful; thank you for your contributions

  20. chino11368
    April 19, 2019 at 17:01

    Hi, I just subscribed to your channel, your video was really helpful! I'm confused about one thing though; at 6:15 you say you're going to pass runIt a function expression, but you don't type var in the parentheses, you go straight to typing the word function. However, in the beginning of the video, you mention that by typing function with out the var, it is a function declaration not a function expression. So is what you're passing at 6:15 to runIt a function declaration or function expression? I want to make sure I understand the fundamentals, so thank you for the help, I really appreciate it.

  21. Jasen Green
    April 19, 2019 at 17:01

    Thank you for the info

  22. Florian Wicher
    April 19, 2019 at 17:01

    Very helpful! I subscribed 🙂

  23. Matthew Hess
    April 19, 2019 at 17:01

    I just subscribed, and am stoked to have this resource by my side learning JS! Great video sir!

  24. Yervand Baghdasarian
    April 19, 2019 at 17:01

    Thank You very much!

  25. Adrián José Alpízar Rojas
    April 19, 2019 at 17:01

    in the call stack you can see the runit function value (that is an expression). so I don't see the difference between then. also, you can call a function with a parameter that is also a function using function declarations. Doesn't it?

Leave A Comment

You must be logged in to post a comment.