Javascript anonymous functions and arrow functions

this challenge confused me a little. it says “In JavaScript, we often don’t need to name our functions, especially when passing a function as an argument to another function. Instead, we create inline functions. We don’t need to name these functions because we do not reuse them anywhere else.” but what does that exactly mean? we still store them in a variable right? so that means we can reuse them. so what does it mean when it says we dont need to name these functions because we do not reuse them anywhere else? need a little help here.

can you give a link to the course?

Right, but storing in a variable isn’t the same as having a name. The storage place is named but the function itself is not.

const func1 = () => {}

const func2 = function() {}

function func3() {}

The first two are anonymous functions that are stored in variable. The last is a named function.

Here:

const transmogrifyData = (d, cb) => d.map(cb)

transmogrifyData(
  data,
  el => el.id = ({ ...el, id: el.id.toString() })
)

this:

el => ({ ...el, id: el.id.toString() })

is an anonymous function. It is being passed to my function.

There it is given a label, “cb”. Is it a named function at that point? I don’t think so - “cb” is just a reference.

Of course, I could abstract it more:

const transmogrifyData = (d, cb) => d.map(cb)

const stringifyIDs = el => ({ ...el, id: el.id.toString() })

transmogrifyData(data, stringifyIDs)

I haven’t tried this, I’m just typing off the top of my head.

Yes, it’s kind of confusing. Don’t worry about it too much - it will gradually make sense over time.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.