6
votes

I have either a typescript or javascript syntax issue. Can someone tell me what _ => this.log... means?

I am used to seeing a name the parameter being passed into the arrow function there.

Does it simply mean 'no parameter'?

Ref: https://angular.io/tutorial/toh-pt6#add-heroserviceupdatehero

    /** PUT: update the hero on the server */
updateHero (hero: Hero): Observable<any> {
  return this.http.put(this.heroesUrl, hero, httpOptions).pipe(
    tap(_ => this.log(`updated hero id=${hero.id}`)),
    catchError(this.handleError<any>('updateHero'))
  );
}
2

2 Answers

8
votes

Its nothing but a notion to name a parameter which isn't going to be used in the function.

Instead, they would have written it like this:

tap(() => this.log(`updated hero id=${hero.id}`)),

If you want to read more, this post is a good start.

12
votes

() => {console.log('Hello World')}

  _ => {console.log('Hello World')}

Both of the above are just the same if your function doesn't need a parameter.

The underscore _ is just a throwaway variable, meaning it can be any variable name since it will never be used. It's just that they usually use the underscore to say that the function doesn't need a parameter.

I write my functions with no parameters using ()=>, but I've seen a lot of versions using the underscore so it's good to understand both.