Arrow/Anonymous Functions
In ES6, there is a new syntax for creating function expressions. It is a short-hand notation.
So far, it's not that big of an improvement, and it does taking some getting used to the new syntax. The real advantage of the new syntax becomes clearer as you add some optional simplifications.
Body block curly braces are not required if there is a single statement.
Notice that the return keyword is not used when the body braces have been removed.
Parentheses are not required for a single parameter
Summarizing Rules for Arrow Functions
Function body block bracing is required if there is more than one statement in the function body. They can be removed if there is only a single statement;
Parentheses surrounding the function parameters are optional if there is a single parameter.
Combining arrow functions with anonymous functions is where their usage really shines as you'll see below.
Anonymous Functions
In the section on function expressions we demonstrated how a function can be passed as an argument to another function. In that case we first defined the function and assigned it to a variable, and then passed the variable to the function.
It is also possible, and more common, to define the function inline, using arrow functions, as I'll demonstrate below. The functions in this case are called anonymous, because they do not have a name and exist only as long as they are used in the context of the function call.
This is how we did it in the previous example. We defined a function expression, assigned it to the variable, and then passed the variable to the doSomething function.
Here, we are passing in an anonymous function expression without having to bother with pre-defining it.
Anonymous functions are an extremely important concept to understand in JavaScript. As an example of their typical usage, the JavaScript Array object has many methods that accept a function to apply to each of the elements in an array.
Multi-line Complex Anonymous Functions
We discussed the rule earlier that the body braces and the return statement weren't required if the body contained a single statement only. The anonymous function can get a bit more complex as there is more involved in the body. Here is an example of how JavaScript typically looks when anonymous functions are used.
The anonymous function is the parameter to the someFunction, and it accepts two parameters. It also has the body braces and a more complex statement than the simple case we saw above when adding two numbers.
Sometimes it helps to pull out the anonymous function and pass it in as a variable to simplify it when you are first getting used to this format.
Or, even convert it back a function expression without the arrow function notation.
We're going to spend a lot of time working with anonymous arrow functions, as that is the way most JavaScript code is written and will be a skill you need to master.
Last updated
Was this helpful?