Function Declarations vs Function Expressions

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024
  • Today we're going to learn about the subtle but important differences between a function declaration and a function expression!
    It's a sneaky little difference but one that is valuable to be aware of!
    ---
    PATREON: / hswolff
    TWITTER: / hswolff
    SITE: hswolff.com/

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

  • @jason0ng
    @jason0ng 4 ปีที่แล้ว +6

    I've watched a few videos explaining this, and yours is by far the most straight to the point and easy to understand! Thank you :D

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

    Not gonna lie i died of laughter at 5:15 when it sounded like he said "fucktion"

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

      😂

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

      I thought i was the only one to notice 😂🤝

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

    I think the two main advantages of function expressions (may regular functions or arrow functions) are:
    1- It forces you to write better your code (depending on hosting is a grubby practice).
    2- It demands less memory heap since the value of your function (your expressions) are not saved in memory until the interpreter reads the line where you initialize your function.
    What do you think about it? Let me know!

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

    Finally I've seen someone who actually knows JS. Thanks for teaching.

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

    I've always figured that functions live in their own space and gets called with the declaration. Though if you put the declaration above and then have a slow (or lazyloaded?) page it will throw an error since it hasn't yet gotten the function in question.
    The reason it doesn't work between declaration and const is because those are things that js will run and they run things from top to bottom.
    Of course you know this, just that it always felt natural for me.

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

      Yeah it can be tricky for people first learning JavaScript as the behavior isn't consistent. That's the part that I dislike the most - all I want are things to be consistent.

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

      @@hswolff = I spent an hour today figuring out why (! + [] + [] + ![]).length is 9.
      Was a good exercise to get a better understanding of how JS is built. Felt almost like when I finally figured out how the stack worked and it stopped being that phantom somewhere in there doing stuff that I kinda learned how to predict.
      Watching your TS videos right now. I'm glad I found you today, finally someone that writes code that are clear and to the point. Looking at other peoples components always feel like overcomplex exercises in code juggling. I appreciate how you seem to see code as a box of tools that you build with rather than a linguistic exercise.
      You deserve a higher viewer base than you have, your content is high quality.

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

      !!! Thank you so much for the kind comment! I'm glad my coding style is clear and easy to follow - it's one of my top priorities!
      I hope my viewer base grows too, I'd love to reach more people :D

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

    Well said, well spoken; thank you !

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

    Another fun fact about function expressions is that you can give them a name, just like a function declaration. Unlike declarations, however, the name is only valid inside the body of the function. For example:
    requestAnimationFrame(function cb() {
    // ...
    requestAnimationFrame(cb)
    })
    cb() // cb is not defined

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

      Wonderful point! Thank you!

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

      what

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

    Nice, straightforward to the point vid. Amazingly hard to find those on YT. People over complicate this subject way too much.

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

      Right? I understand this perfectly now! Every other video I watched literally made it more complicated 😆

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

    Thanks a ton for making this short and sweet video. This was super helpful and straight to the point. Thanks, again 🤝🏼

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

    Which one to use? More common and why? Thnx.

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

    Thanks for sharing your knowledge.

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

    When you say "the variable is not hoisted" is that actually correct? I thought a const variable is hoisted but it's just set to uninitialized?

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

    thank you so much this helps me figuring out why they're different types of function, and now i know! Subscribed!

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

    You're a wizard Harry, thank you. I was having trouble understanding the difference.

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

    whats the use of function expression instead of declaration then?

  • @user-fl9oh4fp8l
    @user-fl9oh4fp8l 3 ปีที่แล้ว

    Thank you, man. Finally got it!

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

    Pretty straight forward nice video

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

    Thank you! Great explanation!

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

    Awesome and easy to understand! SUBSCRIBED !!!!

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

      Woohoo!!

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

    very informative, thanks :)

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

    I like your expressions bro.

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

      and declarations.

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

      Hahaha nice. 😎

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

    that was awesome and easy to understand!
    thank you sir!!

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

      Woohoo!

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

    Super helpful! Thank you.

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

      You are welcome!

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

    Brilliant video! I've just subscribed :D

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

    3:40 pft dude how many times should they tell you to take yo pills

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

    Could you please create a videos on ReactJS PWA

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

    awesome bro

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

      Thanks!

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

    use Extension of vsc Quokka.js
    bcoz output will be display Right of console.log
    no need of run or anything else,

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

      Nice suggestion! Thanks!

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

    expressions > declarations imo

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

      Haha :D