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.

Transcript

Premium

Follow along with the full transcript

Search every word, click any line to jump to that moment, and read along as you watch — a CraftQuest Premium feature.

Sign up for Premium
Craft Version
Craft 4
Topic
Instructor
Level
Intermediate
Date Published
August 22, 2022