3

Suppose I have two functions test1 and test2!

test1 = function(name){
    console.log('Hi, ', name);
};

test2 = function(name) {
    var text = 'Hello ' + name;
    var say = function() { console.log(text); }
    return say;
}

Now I call the function and save them to vars,

var t1 = test1('John');
var t2 = test2('John');

What is exactly happening here?

2
  • test1 will execute the function and print out 'Hi John and return undefined. t1 will be undefined. test2 will return a new function with a closure over text and assign it to t2. t2 is now a new function. You will need to execute it in order to get any output, which you haven't done yet Commented May 2, 2017 at 5:18
  • i think you can call say like t2(); Commented May 2, 2017 at 5:24

4 Answers 4

2

Lets understand this:

In below code you are defining a function called test1 which will print Hi, John if you use test1('John').

test1 = function(name){
    console.log('Hi, ', name);
};

Now lets understand below code, below you are defining a function called test2 which:

test2 = function(name) {
    var text = 'Hello ' + name;
    var say = function() { console.log(text); }
    return say;
}
  • var text = 'Hello ' + name; //define a variable called text
  • var say = function() { console.log(text); } //defininga a function called say which will log the data in variable text in the console.
  • Then you are returning say function.

OUTPUT:
Input: => var t1 = test1('John');
Output: => Hi, John //It's quite straight forward.

Input: => var t2 = test2('John');
Output: => undefined //Here you are re-assigning function returned from test2 which was say to t2. Hence if you now type t2 then you wil get function () { console.log(text); } as output which is similar to say.

I hope you understood, sorry for poor english.

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

Comments

2

Your function test1 returns void, since there is no return statement This function prints Hi John to the console

Your function test2 returns the function say() to the console and stores it in t2. This makes t2 a function. If you pay attention to the syntax in test2 that's one of the two standard ways of defining a function in javascript. if you call

t2(); you will get the similar output to test1('John');:

Hello John

1 Comment

Good and complete answer! One little mistake I found: t2() prints "Hello John", while test1('John') prints "Hi, John". But that is certainly not important for the technical understanding.
1

your first function executes a code and don`t return any thing then t1 will be undefined. but second function returns a function then t2 contains a function that never called so it will not log to console

Comments

1

On calling, test2 returns 'say' which is a pointer to a function, not the value. That is why it will not output anything. To call function 'say' you need to do test2('john')(). In the second pair of parenthesis, we pass in the arguments to the function inside.

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.