2
function a(){
   function b(){
      alert("hi");
   }
   return b();
}
function c(){
   var d = a();
}

c();

When I execute the above, I get the alert "hi". but if I do as below

function a(){
    function b(){
       alert("hi");
    }
    return b();
}
function c(){
    var d = a();
    d();
}

c();

I am expecting to see two alerts for assignment and function call statements in c();, but i get only one alert. What am I doing wrong?

4
  • 2
    Look inside your console. TypeError: undefined is not a function - d is not a function Commented Dec 31, 2013 at 15:30
  • 4
    You aren't returning a function; you're calling it. Commented Dec 31, 2013 at 15:30
  • @rene A question is only off-topic if it doesn't fall within the realm of programming, or is asking for someone to provide you with an entire solution. Just because a problem is relatively easy to solve, doesn't mean that it shouldn't be asked. This is a Q and A website, after all. Commented Dec 31, 2013 at 21:01
  • @nbrooks That might be true, point is when I reviewed this question it had already two close votes for minimal understanding which is not a good reason at all, hence my custom reason for closing. I'm not sure I agree 100% with your position because I still feel that questions that can be solved if an OP uses some of the functionality build-in in the browser or IDE or debugger are off-topic. I retracted my close-vote in favour of your point-of-view. Commented Dec 31, 2013 at 21:12

6 Answers 6

6

Because method a is not returning anything since a is calling b in the return statement but b is not returning anything so d is undefined whcih will cause d() to throw an error

Demo: Fiddle

function c() {
    var d = a();
    alert(d)
    d();
}

If you see the fiddle, the second alert shows undefined as the value of d

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

Comments

4

You are calling an undefined function.

function a(){
    function b(){
       alert("hi");
       // no return => return undefined
    }

    // the result of b() === undefined
    return b();
}
function c(){
    // assign the result of a to variable d
    // (which is itself the result of b, which is undefined, because b has no return)
    var d = a();

    // call undefined function, this will throw an error
    d();
}

c();

Comments

3

The confusion stems from the difference between invoking/calling a function and having a reference to that function. When you add the () to a function reference you are invoking that function which returns the result of executing that function (and not the function itself).

So in this line:

function c(){
    var d = a();
    d();
}

You are assigning d to the result of the function invocation rather than the function reference. As the returned value is not a function itself, the call to d() will fail.

The same also applies in the top function where you have (this seems to be the more likely area of mistake):

return b();

The present code will execute the function and return the result, if the desired behavior was to return a reference to the function itself it should not have the ().

Comments

2

you need to return the function reference, not call it:

return b;

Comments

1

In the second code, you are basically returning the result of the alert, which should be undefined.

var d <= a() <= c() <= b() <= alert('hi') // alert returns undefined

Calling d will throw an error.

Comments

0

If you execute c() in the second example the following will occur:

  • executing function a
  • in a: executing function b
  • in b: executing alert (no return, so undefined is returned)
  • in a: returning undefined value from b
  • var d now contains undefined
  • executing d should throw exception: TypeError: undefined is not a function

Use some tools like chrome's js console to run and debug js.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.