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

JavaScript DOM Scripting By Example Adding and Removing Names Removing Names

Derick Brown
Derick Brown
7,361 Points

Function createLI not an arrow function?

In this video, Guil created a function called createLi:

function createLI() {
}

But in the earlier videos, he created arrow functions.

How come he didn't use an arrow function for "createLI" but instead used the older way of creating functions?

4 Answers

Great question.

When declaring a function, you'll see something like this:

function createLi() {
}

While the function declaration above is syntactically a statement, functions can also be created by a function expression. >Such a function can be anonymous; it does not have to have a name:

When using arrow functions, the function does not have a name (it's always anonymous):

ul.addEventListerner('click', (e) => {

});

This is a great resource that explains functions in greater detail: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Hope this helps!

Derick Brown
Derick Brown
7,361 Points

Thanks for the link and info!

So you will basically only use arrow functions when you're writing function expressions. Correct?

Abraham Juliot
Abraham Juliot
47,353 Points

"When using arrow functions, the function does not have a name (it's always anonymous):"

Actually, arrow functions can have names.

let createLi = () => {

}

Abraham Juliot, I can see what you're saying. But what it looks like you're doing is assigning an anonymous (arrow function in this case) to a named variable. So the function is assigned to the variable.

This is a direct quote from the Mozilla site on arrow functions:

An arrow function expression (previously, and now incorrectly known as fat arrow function) has a shorter syntax compared to function expressions and lexically binds the this value. Arrow functions are always anonymous.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Abraham Juliot
Abraham Juliot
47,353 Points

Hi Brandon, true, I see what your saying.

To add. Named function declarations can be useful when debugging errors. The console will show the function name instead of stating anonymous. This log is what is referred to as the stack trace

To cover another difference between the two involves a topic called hoisting.

A function declarations is hoisted making it available in within the scope. A function expression will only be available after the assignment.

Example of a function declarations:

function createLI() {
// Magic
}

Example of a function expression:

let createLi = () => {
// Magic
}

For more information on hoisting:

MDN Hoisting

Marko Nestorovic
Marko Nestorovic
12,441 Points

If you are going to use function just once, especially for event handler, you don't have function name. But if you are going to reuse it later on, you have to name your function.

For this function, you could write the createLI function like this:

const createLI = text => {

}