145

I am wondering if JavaScript supports writing a function within another function, or nested functions (I read it in a blog). Is this really possible?. In fact, I have used these but am unsure of this concept. I am really unclear on this -- please help!

Jonathan Lam
17.5k17 gold badges72 silver badges99 bronze badges
asked Jul 9, 2010 at 12:19

8 Answers 8

235

Is this really possible.

Yes.

function a(x) { // <-- function
 function b(y) { // <-- inner function
 return x + y; // <-- use variables from outer scope
 }
 return b; // <-- you can even return a function.
}
console.log(a(3)(4));

Alexis
5,8211 gold badge30 silver badges46 bronze badges
answered Jul 9, 2010 at 12:24

3 Comments

This method is called currying.
so are functions a type of object? sorry for necro
@CiY3 They are, via MDN: "In JavaScript, functions are first-class objects, because they can have properties and methods just like any other object. What distinguishes them from other objects is that functions can be called. In brief, they are Function objects."
33

The following is nasty, but serves to demonstrate how you can treat functions like any other kind of object.

var foo = function () { alert('default function'); }
function pickAFunction(a_or_b) {
 var funcs = {
 a: function () {
 alert('a');
 },
 b: function () {
 alert('b');
 }
 };
 foo = funcs[a_or_b];
}
foo();
pickAFunction('a');
foo();
pickAFunction('b');
foo();
answered Jul 9, 2010 at 12:23

3 Comments

Great example. I would add that it's important to note that functions defined inside other functions only exist in that functions scope (unless, of course, you assign a global function to it, as per this example).
Treat those functions like objects they are
why is that nasty exactly? Seems like it can be a useful alternative to a switch case
20

Functions are first class objects that can be:

  • Defined within your function
  • Created just like any other variable or object at any point in your function
  • Returned from your function (which may seem obvious after the two above, but still)

To build on the example given by Kenny:

 function a(x) {
 var w = function b(y) {
 return x + y;
 }
 return w;
 };
 var returnedFunction = a(3);
 alert(returnedFunction(2));

Would alert you with 5.

answered Jul 9, 2010 at 12:30

1 Comment

This method is called currying.
15

Yes, it is possible to write and call a function nested in another function.

Try this:

function A(){
 B(); //call should be B();
 function B(){
 }
}
cнŝdk
32.2k7 gold badges62 silver badges81 bronze badges
answered Mar 19, 2014 at 14:41

Comments

11

Not only can you return a function which you have passed into another function as a variable, you can also use it for calculation inside but defining it outside. See this example:

 function calculate(a,b,fn) {
 var c = a * 3 + b + fn(a,b);
 return c;
 }
 function sum(a,b) {
 return a+b;
 }
 function product(a,b) {
 return a*b;
 }
 document.write(calculate (10,20,sum)); //80
 document.write(calculate (10,20,product)); //250
answered Nov 29, 2013 at 0:13

Comments

5

An alternative solution with ES6 to other answers:

const currying = (x) => (y) => x + y;
console.log(currying(5)(3));

will print to console: 8

answered Nov 13, 2021 at 19:14

1 Comment

A great read on this and a reason as to why it is called 'currying' can be found here javascript.info/currying-partials#currying-what-for
0
function calculate(num1) {
 // arrow function
 return (num2) => num1 + num2;
}
// Invoke the function
console.log(calculate(4)(6));
answered Feb 21, 2022 at 10:45

Comments

0

Is this really possible?

It is very possible. You can do anything with functions in javascript since it is a first class function programming language.

function add(x, y) {
 // we can define another function inside the add function to print our answer
 function print(ans) {
 console.log(ans)
 }
 const ans = x + y
 print(ans)
 return ans
}
add(1, 2)

answered Nov 16, 2022 at 17:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.