Function Declarations VS Function Expressions in JavaScript

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ต.ค. 2024

ความคิดเห็น • 102

  • @alimfuzzy
    @alimfuzzy 6 ปีที่แล้ว +21

    for the first time I understand hoisting. thanks

    • @alijouini4437
      @alijouini4437 6 ปีที่แล้ว +2

      read closures and scopes by kyle simpson ; it's free

  • @ChrisTian-ox5nr
    @ChrisTian-ox5nr 4 ปีที่แล้ว +4

    Thank you! Great tutorial. Love how you extract the code first and then use it as an example. Thank you definitely suits my style of learning.

  • @jonsnow9246
    @jonsnow9246 6 ปีที่แล้ว +5

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

  • @sonnychiba5270
    @sonnychiba5270 5 ปีที่แล้ว +2

    Simple, clear explanations with practical examples, can't beat that, great video

  • @AndreGMendes
    @AndreGMendes 5 ปีที่แล้ว +3

    Thank you! I can now finally see the light

  • @Pareshbpatel
    @Pareshbpatel 3 ปีที่แล้ว +1

    Very clear tutorial on Function Declarations vs Function Expressions. Thank you.
    {2021-9-17}

  • @appleqor
    @appleqor 6 ปีที่แล้ว +2

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

  • @doktora9292
    @doktora9292 7 ปีที่แล้ว +53

    Very good video, but next time check audio quality. :)

  • @germanduterte7110
    @germanduterte7110 4 ปีที่แล้ว

    I feel very thankful because this is the exact explanation that I need

  • @kamleshpawar9483
    @kamleshpawar9483 3 ปีที่แล้ว

    very nicely explained.

  • @kineticBoss
    @kineticBoss 6 ปีที่แล้ว +13

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

  • @dmytromoroz4451
    @dmytromoroz4451 4 ปีที่แล้ว

    Thanks for comprehensive explanation!

  • @exikoz
    @exikoz 5 ปีที่แล้ว

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

  • @rfcosta85
    @rfcosta85 3 ปีที่แล้ว

    Very Good! Thanks!

  • @alijouini4437
    @alijouini4437 6 ปีที่แล้ว +1

    much appreciation goes forever

  • @webstuff56
    @webstuff56 7 ปีที่แล้ว +2

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

    • @AllThingsJavaScript
      @AllThingsJavaScript  7 ปีที่แล้ว +2

      Here is a tutorial on closures I've already published: th-cam.com/video/TznpOmv2BQM/w-d-xo.html

    • @webstuff56
      @webstuff56 7 ปีที่แล้ว

      Thank you

  • @huylearning2543
    @huylearning2543 ปีที่แล้ว

    it's a good explain video, thank you

  • @ashishnayak2666
    @ashishnayak2666 4 ปีที่แล้ว

    nice explanation

  • @Zen-lz1hc
    @Zen-lz1hc 3 ปีที่แล้ว

    Great advice !
    Thanks

  • @ezouras
    @ezouras 6 ปีที่แล้ว +1

    Great examples. Thanks!

  • @mohammadcrativity1818
    @mohammadcrativity1818 2 ปีที่แล้ว

    Thank you !Great

  • @RafaelSFTech
    @RafaelSFTech 5 ปีที่แล้ว

    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

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว

      I don't know of any way to do an IIFE without an expression. In fact that is part of the name: Immediately Invoked Function Expression. If you are doing something besides declaring a function, it is an expression. Maybe it is just a matter of semantics. If you want you can paste your code.

  • @siddisking
    @siddisking 4 ปีที่แล้ว +1

    I knew hoisting but couldn't relate that hoising actually created a diff b/w anonymous and named functions
    Good job stating that :D

  • @amirhosseinbidokhti
    @amirhosseinbidokhti 5 ปีที่แล้ว +1

    Wow, easily explained, super helpful. I can't wait to watch your other videos.
    Also, does it make sense that function expression is more efficient with computer memory? Because it is not already defined before the execution phase?

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว +1

      I've not seen any stats on this, so I'm not sure if either one is more efficient memory wise.

  • @pavankumar-sq7nc
    @pavankumar-sq7nc 5 ปีที่แล้ว +1

    Thank you Sir.

  • @arijitbanerjee6442
    @arijitbanerjee6442 7 หลายเดือนก่อน

    Great content! But I have a doubt. Is a function expression an executable statement? Can you please elaborate on this? Thanks in advance.

    • @AllThingsJavaScript
      @AllThingsJavaScript  7 หลายเดือนก่อน

      Yes, the executable statement declares the function. That is the part that is executable.

  • @rastkogojgic699
    @rastkogojgic699 2 ปีที่แล้ว

    Thanks, great video. One question:
    Technically, we could have also created a function declaration, and then passed that function as an argument inside of RunIt function. But I guess that's just too much redundant code...

  • @jovannikolic6346
    @jovannikolic6346 4 ปีที่แล้ว

    Thanks :)

  • @spongechameleon6940
    @spongechameleon6940 4 ปีที่แล้ว

    Could you go over the meaning of the 'this' keyword in function declarations and in function expressions? Particularly for declarations (methods) and expressions in an object? Could you also clarify how an arrow function changes the meaning of 'this' in each of those contexts? Great video, thank you.

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +1

      Here are a couple of tutorials on this in arrow functions: th-cam.com/video/dAAVtx-YW44/w-d-xo.html and th-cam.com/video/bcSnZlC-9SY/w-d-xo.html These may answer your other question as well because it is compared to regular functions.

  • @yervandbaghdasaryan8156
    @yervandbaghdasaryan8156 7 ปีที่แล้ว +1

    Thank You very much!

  • @itsjmmariano
    @itsjmmariano 4 ปีที่แล้ว

    Thank you for sharing this! :)

  • @damo190
    @damo190 2 ปีที่แล้ว

    Sir I checked from mdn, sometimes function declaration turns into function expression when the function declaration becomes part of an expression or if is no longer a source element of a function or the script itself. Will u clarify it?

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 ปีที่แล้ว

      Both of those are function declarations. If you are ever unclear, try eliminating the function name. Not allowed with a function declaration. Is allowed with a function expression.

  • @21agdmnm
    @21agdmnm 4 ปีที่แล้ว

    question: the arrow functions are basically function expressions correct? theres no such thing as arrow function declaration? am I right ?

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +1

      You are correct. All arrow functions are function expressions.

    • @21agdmnm
      @21agdmnm 4 ปีที่แล้ว

      @@AllThingsJavaScript thx!

  • @jasengreen697
    @jasengreen697 7 ปีที่แล้ว

    Thank you for the info

  • @Martin958
    @Martin958 2 ปีที่แล้ว

    Why does the call stack list the run function at line 22? Looking at your code it is line 21.

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 ปีที่แล้ว

      Not sure why it is off by 1 line, but it is close enough to find it. Probably a chrome thing.

  • @lonacmoric6998
    @lonacmoric6998 3 ปีที่แล้ว

    It's a shame that you don't have more views on your videos

  • @mukeshchouhan9475
    @mukeshchouhan9475 5 ปีที่แล้ว

    Could you please create a video on currying && pure functions. Or a series over functional programming in JS.

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว

      Functional programming is difficult to address in a single tutorial. I've done a course on it: www.udemy.com/functional-programming-in-javascript-a-practical-guide/?couponCode=TH-cam19

  • @pavleth14
    @pavleth14 3 ปีที่แล้ว

    I try, and function declaration do exactly the same job as function expression in this case:
    function runIt(a) {
    console.log(a);
    a();
    }
    runIt(function(b) {
    b = 'now';
    console.log('Running ' + b);
    });
    I don't get it, why is so important to write that function as function expresson, and what is wrong with function declaration in this case? Just asking :))

    • @AllThingsJavaScript
      @AllThingsJavaScript  3 ปีที่แล้ว

      There is nothing wrong about function declarations. You just need to understand the difference. The reason I prefer function expressions is because they show the true nature of functions in JavaScript. They are first class. But I also use plenty of function declarations. th-cam.com/video/1ieC6JZmF5w/w-d-xo.html

  • @paulca55
    @paulca55 6 ปีที่แล้ว

    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!

    • @AllThingsJavaScript
      @AllThingsJavaScript  6 ปีที่แล้ว +3

      In certain situations it is simply a choice which one you use. Some situations you are required to use a function expression, like when a function is part of some greater expression (IIFEs). Function declarations are hoisted and so I prefer function expressions because it forces me to structure my code better (e.g. need to declare the function before I use it.) Hopefully, that gives you some reasons to think on.

    • @paulca55
      @paulca55 6 ปีที่แล้ว

      Yeah that makes sense, thanks for the prompt reply. I thought you might of been saying that to pass a function into another function it would need to be a function expression.

  • @florianwicher
    @florianwicher 7 ปีที่แล้ว

    Very helpful! I subscribed :)

  • @chino-ir8sy
    @chino-ir8sy 7 ปีที่แล้ว

    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.

    • @AllThingsJavaScript
      @AllThingsJavaScript  7 ปีที่แล้ว +3

      It is a function expression. Perhaps a better way to define a function expression is that it is a function that is declared as a part of an expression. var fun = function() {} is just the normal way this happens. But when we pass a function to another function, it is also part of an expression. A function declaration is not part of some expression. Does that help?

    • @chino-ir8sy
      @chino-ir8sy 7 ปีที่แล้ว

      All Things JavaScript, LLC yes that definitely clears things up. Thank you again for the help!

  • @whiteeye2121movalik
    @whiteeye2121movalik 4 ปีที่แล้ว

    seems to me it's always better to use function declaration because of the hoisting advantage. when is it better to use function expression instead?

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +1

      I prefer function expression because it forces me to organize my code. Plus function expressions express the true nature of functions. As they are assigned to a variable I can then pass them around etc. Finally, arrow functions (the latest syntax) are function expressions. I hope that helps!

  • @sebastiankumlin9542
    @sebastiankumlin9542 4 ปีที่แล้ว

    Great!

  • @tamirmoav3774
    @tamirmoav3774 3 ปีที่แล้ว

    thank you. I wrote this code:
    document.getElementById("myref").onclick = function ()
    {
    document.getElementById("par").innerHTML="hello world";
    }
    press
    and nonthing happens only if I use the old JS method like:
    why is that?

    • @Galaron1000
      @Galaron1000 ปีที่แล้ว

      The reason the code in the script tag is not working when the button is clicked is that it is being executed before the document is fully loaded. This means that the JavaScript code is looking for the HTML element with the ID "myref" before it has been fully loaded into the browser's DOM.
      One solution to this problem is to place the script tag after the HTML elements it references, or to wrap the JavaScript code inside an event listener that waits for the document to finish loading before executing. This ensures that the JavaScript code has access to all the HTML elements it needs to interact with.

  • @admoncho
    @admoncho 7 ปีที่แล้ว

    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?

    • @AllThingsJavaScript
      @AllThingsJavaScript  7 ปีที่แล้ว +1

      Yes, you can pass a function to a function whether or not it is declared as a function expression or function declaration. The main differences: obviously the structure, but also how hoisting affects each one and whether or not a name is assigned to the function. A function expression is usually unnamed. (Though it doesn't have to be.)

    • @admoncho
      @admoncho 7 ปีที่แล้ว

      thank you very much. awesome videos!

  • @HarryManchanda
    @HarryManchanda 7 ปีที่แล้ว +1

    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`

    • @HarryManchanda
      @HarryManchanda 7 ปีที่แล้ว

      also same goes for let ....
      You can learn more about it here aka Temporal Dead Zone
      stackoverflow.com/a/31222689/3301436

    • @AllThingsJavaScript
      @AllThingsJavaScript  7 ปีที่แล้ว +1

      Thanks for contributing Harry!

  • @MsElsospechoso28
    @MsElsospechoso28 5 ปีที่แล้ว

    I would give you a million likes if I could

  • @brucelee7782
    @brucelee7782 5 ปีที่แล้ว +1

    I still dont get why would I prefer one over another

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว +1

      For me function expressions express the true nature of functions; that they are objects that can be passed around and stored in variables. It also forces my code to be better organized because a function expression is not hoisted; so I must define the function before I use it.

  • @shashidharpatil9388
    @shashidharpatil9388 4 ปีที่แล้ว

    function funcD() {
    console.log("Function Declration");
    }
    display(funcD);
    function display(f) {
    f();
    }
    using the function declaration gave me the same results.Can somebody confirm that even function declarations can be passed as parameters ?

    • @AllThingsJavaScript
      @AllThingsJavaScript  4 ปีที่แล้ว +1

      Yes, function declarations can be passed to other functions. Function expressions just assign the function to a variable from the get go.

    • @shashidharpatil9388
      @shashidharpatil9388 4 ปีที่แล้ว

      Thankyou for the reply 😁. Could you throw some light on when to use function declaration and when to use function expression ?

  • @amguruprasath8037
    @amguruprasath8037 5 ปีที่แล้ว

    In which condition do we use function expression?

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว

      I always use function expressions. I believe it forces you to organize your code better. But it is important to understand function declarations as you will encounter them.

    • @AllThingsJavaScript
      @AllThingsJavaScript  5 ปีที่แล้ว +1

      Also, certain situations require expressions, so you are forced to use them.

  • @edemawuku2652
    @edemawuku2652 2 ปีที่แล้ว

    Can someone please help me understand it? I just started learning javascript and everyone in the comment section seems to understand it...

    • @AllThingsJavaScript
      @AllThingsJavaScript  2 ปีที่แล้ว

      So basically this is two ways to declare functions. Which part is unclear to you?

  • @goat5249
    @goat5249 4 ปีที่แล้ว

    WHO WON? YOU DECIDE!

    • @goat5249
      @goat5249 4 ปีที่แล้ว

      8:37

  • @jonsnow9246
    @jonsnow9246 6 ปีที่แล้ว +2

    Please make a tutorial on react!

  • @notconvinced2204
    @notconvinced2204 4 ปีที่แล้ว

    The three little semicolons are making the same faces I’m making while trying to find a video about functions that doesn’t assume I know anything about js

  • @firstname-nq7qc
    @firstname-nq7qc 3 ปีที่แล้ว

    nice explaining but that audio is horrible. :(

  • @BobbyBundlez
    @BobbyBundlez 4 ปีที่แล้ว +1

    cmon bro. great vid but this audio quality is TRASH.

  • @luisbetancourt3837
    @luisbetancourt3837 4 ปีที่แล้ว

    Fix your audio!

  • @functionalprogramming2352
    @functionalprogramming2352 4 ปีที่แล้ว

    Who cares about the audio.

  • @jtvlogs6711
    @jtvlogs6711 5 ปีที่แล้ว +1

    who else is completely lost?

  • @sagfag3984
    @sagfag3984 5 ปีที่แล้ว

    Why you yellin’?

  • @mortyjr2334
    @mortyjr2334 5 ปีที่แล้ว +1

    JavaScript may just be the ugliest language I have ever seen lol.