liveBook · Manning

Arrow function

As explained in Pro Angular 16

An arrow function is JavaScript function syntax with lexical this binding and a concise expression form.

An arrow function is concise JavaScript or TypeScript function syntax. It places input parameters before an arrow and the function result after it. The syntax is also called fat arrow syntax or lambda expression syntax. section 4.2.5

Use arrow functions to pass behavior or define short functions inline:

  • Describe a function parameter with a type such as () => string: the function accepts no parameters and returns a string. section 4.2.5
  • Pass a named function without parentheses when another function should invoke it later. section 4.2.5
  • Use an inline arrow function such as () => "Paris" when the behavior is short and specific to the call. section 4.2.5
  • Capture an accessible value in an arrow function, as in () => myCity, to defer producing that value until invocation. section 4.2.5

Supplying a city-producing function

function writeCity(f: () => string) { console.log(`City: ${f()}`); } writeCity(getUKCapital); writeCity(() => "Paris"); let myCity = "Rome"; writeCity(() => myCity); The first call supplies a named function and prints London. The second supplies an inline arrow function and prints Paris. The third uses a closure and prints Rome. Calling getUKCapital() instead would pass its result rather than the function itself, producing a type mismatch for writeCity. section 4.2.5

Filtering incomplete todo items

get itemCount(): number { return this.list.items .filter(item => !item.complete).length; } Here, item => !item.complete concisely expresses the filtering function: each item produces the Boolean result !item.complete. The resulting array length supplies the number of incomplete items. section 2.3.3

Listing 4.8. Defining an arrow function in the main.ts file in the src folder
function getUKCapital() : string { 
    return "London";
}
 
function writeCity(f: () => string)  {
    console.log(`City: ${f()}`)
}
 
writeCity(getUKCapital);
writeCity(() => "Paris");

Inspect a complete runnable example that passes both a named function and an inline arrow function to another function. section 4.2.5

How Pro Angular 16 covers it

How deep it goes
Explains parameters, return types, invocation, inline definitions, closures, and filtering, but does not develop broader function semantics.
Examples
Demonstrates city-producing functions, Paris and Rome closures, and filtering incomplete todo items.
What it assumes
Assumes basic JavaScript or TypeScript functions, parameters, return values, arrays, and Angular component code.
What you can do after
Apply it via concise callbacks, typed function arguments, inline functions, and closure-based functions.
How it connects
Connects arrow functions with higher-order functions, callbacks, closures, function types, arrays, filtering, TypeScript, and Angular components.

Also explained in

Compare how 7 books explain arrow function →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.