Understand : Arrow Functions to Write Concise Anonymous Functions

Hello !
I’m doing the ES6 course - Use Arrow Functions to Write Concise Anonymous Functions for the second time, and I need some explanation.

  • I can not understand why write a function if it is to return a value?
    Why not give it this value directly?

  • Why create a function if it’s not reused? Is not this the purpose of a function?

Thank you in advance for your help
Quentin

functions can hold a lot of things inside its body , like it can hold some logic after returning a value ,
you can use anonymous function as callback ,or for example in addEventListener(here the event name, anonmymous function)
I think the example they used where they are returning just a value ,they want to just demonstrate the different structure between to ways of writing anonymous functions ,arrow function and a simple function .

1 Like
  • can not understand why write a function if it is to return a value?
    Why not give it this value directly?

Assume that you wish to describe a procedure or formula that does some kind of transformation to the input, regardless of the input. Say, add 5 or multiply by 3. This is capture in a function. It does the same transformation but the value returned depends on the value that is inputed.

  • Why create a function if it’s not reused? Is not this the purpose of a function?

Anonymous functions might very well be re-used. The forEach, map, filter and accumulate all use an anonymous function, but it runs once for each element in the array.

1 Like