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
25 responses to “Function Declarations VS Function Expressions in JavaScript”
Thank you Sir.
I would give you a million likes if I could
I still dont get why would I prefer one over another
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
who else is completely lost?
Dude thank you for saving me from this headache i got for trying to understand the difference between these two.
Please make a tutorial on react!
Make this channel famous!
Don't stop making videos!
Solid explanation. The audio was good enough for me!!
much appreciation goes forever
There was some issue with the audio, but regardless great explanation as always
Check out Atom Editor.
for the first time I understand hoisting. thanks
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!
Great examples. Thanks!
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`
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
Very good video, but next time check audio quality. 🙂
closures and callbacks would be helpful; thank you for your contributions
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.
Thank you for the info
Very helpful! I subscribed 🙂
I just subscribed, and am stoked to have this resource by my side learning JS! Great video sir!
Thank You very much!
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?