0

Here's the function code:

function TestFunction(number){
  return function(e){
    return `${number}`;
  }
}

When I use it on google's devtools command line it returns:

function(e){
  return `${number}`;
}

So it looks like the function returned is not created with the number I give to TestFunction, instead it takes the string just like it was written. I have tried to use concatenation instead of interpolation but still not working. What can I do?

2
  • The value of number is resolved when the function is actually executed. Commented Dec 14, 2019 at 9:57
  • @FelixKling You're right, thank you I didn't know that Commented Dec 18, 2019 at 19:03

1 Answer 1

1

There is indeed a closure around the second function, so it will have memory of what num is.

function a(num) {
  return function b() {
    return `${num}`;
  }
}

const c = a(6);

console.log(c());

Sign up to request clarification or add additional context in comments.

Comments

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.