Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Owa Aquino
19,277 PointsJavaScript : What's the difference between this two functions'?
Hi Community!
Well I've seen these two functions setup in the JavaScript courses. Dave and Andrew were using different function approach.
Dave uses:
function fName(a) {
}
while Andrew is using it like this
var fName = function(a){
}
Just would like to ask what's the difference and when best to use Andrew or Dave ways?
Thanks!
3 Answers
Steven Parker
243,656 Points
The difference is subtle, but it may be important.
In the first case, a function is created with the name fName.
In the second case, an anonymous function is created (it has no name), but a variable fName is assigned with it.
Now in both cases, you can call fName() after it has been defined, but in the first case, you can also call it before it has defined. This is due to a principle of JavaScript known as "function hoisting". This causes function definitions to behave as if they were at the top of the script. But assignments of functions to variables only happen where they are written.
So, for example:
// ------ function declarations are hoisted ------
a = fName(); // <-- legal !!
function fName(a) {
}
b = fName(); // <-- legal
// ------ variable assignments are NOT hoisted ------
a = fName(); // <-- NOT legal (undefined)
var fName = function (a){
};
b = fName(); // <-- legal
Alexander Davison
65,469 PointsThere's a longhand way and a shorthand way to write functions. I usually use the shorthand way because it makes a little more sense to me.
Here's an example of the shorthand function:
function myFunc() {
}
The longhand way is like this:
var myFunc = function () {
};
As you can see, in the longhand version, we explicitly create a variable and assign the function to the variable, so myFunc appears before the function keyword. By contrast, the function keyword appears first in the shorthand version, the variable myFunc is created by JavaScript behind the scenes. Hope this makes sense :)
Good luck, Alex
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsThe 2nd function is an anonymous function.
Check out this link for more information :-) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions
Owa Aquino
19,277 PointsOwa Aquino
19,277 PointsIn my understanding.. if im going to use the Case 1 I should first declare a variable above it? and if im going to use Case 2 I don't have to to declare a separate variable? Is that correct?