2

I have a simple question here. Here is the code below

    var num = 0;

    var increment = function() {
        return function() { num++; };
    };

    increment();
    increment();

However, when I tried to run it, it errors with undefined is not a function. How come? Isn't increment clearly a function?

Also, when I wrote typeof increment, it returns undefined.

When increment() is called twice, it should modify num, and become 2.

5
  • It works for me in Chrome console. (As an aside, why not simply define var increment = function() { num++; }? Commented May 27, 2014 at 20:16
  • This code looks right, although it's strange you return a function from increment() Commented May 27, 2014 at 20:18
  • I purposely made it like that a function inside function, just to test a case. With that kind of form, how can I make the function works? Commented May 27, 2014 at 20:20
  • I doubt this code will throw such an error. Commented May 27, 2014 at 20:25
  • As @TedHopp pointed out, in which environment do you execute this ? Commented May 27, 2014 at 20:27

7 Answers 7

2

I'm guessing you want to do something like this:

> var makeIncrement = function () { var num = 0; return function () { return num++ } }
undefined
> increment = makeIncrement()
function () { return num++ }
> increment()
0
> increment()
1

The function has to be called (makeIncrement()) in order to return the inner function, which you can then assign to a variable. Once it has been assigned, you can call it and it will work as you expect.

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

3 Comments

@user122652 no problem, happy to help. By the way, I just edited to use var num so the num variable isn't visible to the rest of the code. This means that you could have several increment functions, each with their own independent counter.
@user122652 if your problem is solved, don't forget to accept an answer and upvote any answers that you found useful.
@user122652 you can vote for as many answers as you want. Upvoting means that you found the answer useful. To accept your favourite answer, you click the outline of a tick next to the answer. It will turn green, which means that the answer has been accepted.
0
var num = 0;
var increment = function() {
    num++;
};
increment();
increment();

1 Comment

I want to make it a function inside a function
0

but what are you returning with return function()? You should just return num.

    var num = 0;
    var increment = function() {
    num++
        return num; 
    };
    var something = 0;
    something = increment();
    something = increment();
alert(something);

1 Comment

I want to make it a function inside a function
0

As per your comment, if you want to return a function from your function, call the function returned by increment() :

var num = 0;

var increment = function() {
    return function() { num++; };
};

increment()();
increment()();

Or, better suited :

var incrementer = increment();
incrementer(); //Call this each time you want to increment num

1 Comment

I want to make it a function inside a function
0

It's not a function ... exactly. Your function "increment" returns a function, not the incremented value. The correct way should be: var increment = function() { return num++; };

Phil

1 Comment

increment looks like a function to me. What it returns doesn't matter.
0

Yes, the increment function does not increment the variable; it returns an inner function that would increment the variable when invoked.

As written, you would have to invoke the result of invoking the increment function:

increment()(); //results in num++

OR

var incrementFunction = increment(); //does not increment
incrementFunction(); //num++

Comments

-1

If you want to assign to increment the inner function, then you need to execute the outer function:

var increment = (function() {
    return function() { num++; };
}());

(This is called an immediately-invoked function expression, or IIFE for short.) A simpler approach, of course, is:

var increment = function() { num++; };

but perhaps you have other reasons for wanting to do it in a more complicated way.

2 Comments

Hi Tedd. I tried it and it still says undefined is not a function
@user122652 - What environment are you using to execute this?

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.