1

new to Javascript, I am going through closures and just wondering What the outer function variable and outer function is in this closure?

function hello(name, age) {
  var message = name + ", who is " + age + " years old, says hi!";

  return function sayhi() {
    console.log(message);
  };
}

var sayHelloToJohn = hello("John", 33);

sayHelloToJohn();
connexo
  • 53,704
  • 14
  • 91
  • 128
  • [How do JavaScript closures work?](https://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Andreas Feb 22 '20 at 09:40

1 Answers1

1

In your example, the outer function variable is message and its params name age, and the outer function is hello.

It's about their relative with the function sayHi. So sayHi can use its outer function variable, it knows message and prints it out as you can see.

thanhdx
  • 608
  • 4
  • 16