#returning functions

4 messages · Page 1 of 1 (latest)

gritty ember
#

How come u don't need a function name when u return said function within another function?

azure birch
#

Can you post an example snippet of exactly what you mean? Like this?

    function someFunction() {
        // like this?
        // are you asking why the anonymous function below can be returned without naming it?
        return ((a, b) => {
            console.log(`sum is: ${a + b}`);
        });
    }
    
    const f = someFunction();
    f(2, 3);
#

If so, I believe the answer might be because functions in JavaScript are first-class citizens, so they behave just like Numbers, Strings, etc.? e.g. When you return a Number from a function, you can just do return 42; rather than const value = 42; return value;.

gritty ember
#

oh, i just found out about anonymous functions. that's what i was describing and i didn't know it. thanks ❤️