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/
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
Not gonna lie i died of laughter at 5:15 when it sounded like he said "fucktion"
😂
I thought i was the only one to notice 😂🤝
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!
Finally I've seen someone who actually knows JS. Thanks for teaching.
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.
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.
@@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.
!!! 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
Well said, well spoken; thank you !
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
Wonderful point! Thank you!
what
Nice, straightforward to the point vid. Amazingly hard to find those on YT. People over complicate this subject way too much.
Right? I understand this perfectly now! Every other video I watched literally made it more complicated 😆
Thanks a ton for making this short and sweet video. This was super helpful and straight to the point. Thanks, again 🤝🏼
Which one to use? More common and why? Thnx.
Thanks for sharing your knowledge.
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?
thank you so much this helps me figuring out why they're different types of function, and now i know! Subscribed!
You're a wizard Harry, thank you. I was having trouble understanding the difference.
:D
whats the use of function expression instead of declaration then?
Thank you, man. Finally got it!
Pretty straight forward nice video
Thank you! Great explanation!
Awesome and easy to understand! SUBSCRIBED !!!!
Woohoo!!
very informative, thanks :)
I like your expressions bro.
and declarations.
Hahaha nice. 😎
that was awesome and easy to understand!
thank you sir!!
Woohoo!
Super helpful! Thank you.
You are welcome!
Brilliant video! I've just subscribed :D
3:40 pft dude how many times should they tell you to take yo pills
Could you please create a videos on ReactJS PWA
awesome bro
Thanks!
use Extension of vsc Quokka.js
bcoz output will be display Right of console.log
no need of run or anything else,
Nice suggestion! Thanks!
expressions > declarations imo
Haha :D