0

I have two examples of simple closures and my question is that when should I return the function with or without the bracket? Thank you!

Example 1:

function names (first, last) {
    var intro = "My name is ";
    function full_Name () {
        return intro + first + " " + last;
    }
    return full_Name();   <----
}

full_Name("Macro","phages"); // My name is Macro phages

Example 2:

function names (first) {
    var intro = "My name is ";
    function full_Name (last) {
        return intro + first + " " + last;
    }
    return full_Name;    <---- why not full_Name();
}

var white_cells = names("Macro");
white_cells("phages");  // My name is Macro phages
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
Javagaming
  • 13
  • 2

1 Answers1

1

return foo(); calls the function and returns its return value (which is intro + first + " " + last; in your example).

return foo; returns the function itself.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143