0

Here is my code

function first() {
  
  console.log("first")
  
  return function second() { return console.log("second") }
}

let foo = first() // It is outputing "first" once

foo() // Output is "second"
first()() // Output is "first" "second"

What I do not understand is why foo() is giving a different output comparing to first()(). If foo = first(), then I think it should have the same output.

The second question (and less important) is: how can I avoid the output when assigning the variable let foo = first() ?

3
  • Because "First" has already been logged to the console, when you assigned foo to the result of first() Commented Aug 23, 2022 at 12:36
  • 4
    first() is calling first. You're executing the code in that function, which includes the console.log... () isn't "access". It's "Run this". Commented Aug 23, 2022 at 12:37
  • it's High order function which is a function that return a function or a function that takes a function as an argument,when u do first()() you invoke the first function and the second function Commented Aug 23, 2022 at 12:40

2 Answers 2

2

It's because you are not assigning first to foo when you do first(), you are calling first. When first is called, it logs "first" to the console and then it returns the function second. So foo gets assigned the return value of first which is the function second.

Assigning first to foo

You can assign the function first to foo via normal assignment without calling it:

let foo = first; // now foo === first

Assigning the returned function second to foo

let foo = first(); // note the use of (); now foo === second

Immediately calling the returned function

In the last line, you are doing two things in one line. The first first() (no pun intended) returns the second function (and thereby executes the console.log statement) which is immediately called via () (and executes the other console.log statement). That's why you see it logging "first"and "second".

let foo = first()(); // note the doubled use of (); now, foo === undefined

Whatever are we doing here?

Although it might seem a bit strange at first, the technique of passing and/or returning a function to/from another function is very useful - so much that it has a name: Higher Order Function (or - as I like to call it - HOF). One pretty common example of a HOF is Array.prototype.map. When you pass a function to .map, it projects the function onto every element in the array and collects the resulting value of each projection into a new array which it finally returns.

A concrete example of a HOF besides .map: Suppose you have to call .split on different input strings that come from different sources but you always want to split the strings on the same character. Create a makeSplitter HOF:

function makeSplitter(splitOn) {      // <-- HOF
  return function (inputString) {     // <-- return value of HOF
    return inputString.split(splitOn);
  }
}

With this, you can create a general function that splits an input string, say, by slash:

const splitSlashes = makeSplitter(/\//g);

You can think of splitSlashes as if it had been written like so:

const splitSlashes = function (inputString) {
  return inputString.split(/\//g);
}

And now you can use that to split various strings that contain slashes into separate parts:

 console.log(splitSlashes('08/12/2022')); // logs ['08', '12', '2022']
 console.log(splitSlashes('www.example.com/path/to/x/')); // logs ['www.example.com', 'path', 'to', 'x']

If you need another function that - for example - splits a string on ., just use makeSplitter with another argument:

const splitDots = makeSplitter(/\./g);
Sign up to request clarification or add additional context in comments.

Comments

1

Output of a function is a value in return statement but not console.log in a function body

function first() {
  return function second() { return 'second' }
}

let foo = first() //now a function is in foo variable

foo() // "second"
first()() // "second"

the same output in both cases

2 Comments

You are correct. It is not an output but a side effect. Thank you for pointing that out.
np!,yeap, exactly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.