What are Arrow Functions in Twig?

Andrew gives an overview of arrow functions in Twig.

What is an arrow function?

  • An arrow func­tion is a short­hand way to write an anony­mous function
  • An anony­mous func­tion is a func­tion with­out a name or identifier
  • Arrow func­tions have implic­it return values

Here’s an exam­ple of an arrow func­tion (in gener­ic code):

	(value) => value * 5

This is what it would look like as a reg­u­lar function:

	function calc(value) {
		return value * 5
	}

As you can see, the arrow func­tion ver­sion is a more suc­cint way of writ­ing the same thing. Notice that there’s no explic­it return state­ment in the arrow func­tion, like there is in the sec­ond example.

Craft Version
Craft 4
Topic
Instructor
Andrew Welch
Level
Intermediate
Date Published
August 22, 2022
Andrew Welch

Andrew is a regular contributor to CraftQuest through the CraftQuest on Call livestream. He's the developer behind the most popular Craft CMS plugins, like SEOmatic and Retour. Andrew has decades of experience in software development for both web and client applications.