Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this video, we'll convert our anonymous function into an arrow function.
Arrow Functions
( ) => {
console.log("Hello");
}
( ) => console.log("Good bye");
Checkout this workshop on Arrow Functions for more details and examples.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Arrow functions are a common way you'll
see anonymous functions being written.
0:00
In this video, we'll convert our
anonymous functions into arrow functions.
0:05
Let's pull out this anonymous function
from the executeCallback function.
0:10
This way, we can see the progression
from a regular anonymous function
0:15
to an arrow function.
0:19
To convert a function
into an arrow function,
0:20
simply remove the function keyword.
0:24
And after the parentheses, add an equal
sign and a greater than symbol,
0:26
this is often called a fat arrow.
0:31
Let's reintroduce the callback
into the executeCallback function.
0:34
Arrow functions have an even shorter
syntax, if there's a single line of code,
0:41
you can remove the curly braces, like so.
0:46
Why not pause the video and
create an arrow function for
0:52
the goodbye anonymous callback?
0:55
How did you do,
it's pretty straightforward,
1:01
remove the keyword, and add the fat arrow.
1:06
You could have gone a step forward and
removed the curly braces too,
1:10
you'd also need to remove the semicolon.
1:17
You need to remove the semicolon
after the console.log statement,
1:22
because it would terminate the line
of code before the last parentheses.
1:26
If the line of code terminated
before the parentheses,
1:31
the executeCallback function
wouldn't get executed.
1:34
Well done, you've created your own
callback function and callback executor,
1:40
and used two styles of
anonymous functions.
1:44
So far, the example we have
used is pretty contrived.
1:47
In the next few sections, we're going to
take a look at some real world examples
1:51
using timers and a document object model.
1:55
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up