# Javascript - How does this preservation of variable works in Closure javascript? [closed]

function outside(x) {
function inside(y) {
return x + y;
}
return inside;
}
fn_inside = outside(3); //1* -  Think of it like: give me a function that adds 3 to whatever you give it
result = fn_inside(5); //2* - returns 8

result1 = outside(3)(5); // returns 8


1*,2* - Since each call provides potentially different arguments, a new closure is created for each call to outside. The memory can be freed only when the returned inside is no longer accessible.

Questions

1) What happens when we call outside(3)?

• 1a) What does it return?

2) Is fn_inside(3); a valid function call?

3) How is outside(3)(5) valid when outside function definition accepts only one parameter?

## closed as off-topic by πάντα ῥεῖ, Billal Begueradj, Phrancis, Ludisposed, t3chb0tJun 27 '18 at 10:12

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – πάντα ῥεῖ, Billal Begueradj, Phrancis, Ludisposed, t3chb0t
If this question can be reworded to fit the rules in the help center, please edit the question.

• Welcome to Code Review. This question doesn't reflect what the site is about. It's not on-topic to ask for an explanation of code that you do not understand. See What topics can I ask about?. – Phrancis Jun 27 '18 at 6:03
• @Phrancis - It says what questions should be asked here. But it doesn't say where this kind of question go to . When I go to Stackoverflow, people kick me out there the same way . So I came here and people do the same. Please take this issue seriously and come up with a tabular column or AI to recognise and categorise the question. It would definitely help the community. – sofs1 Jun 27 '18 at 9:06
• Your question definitely does not fit Code Review, and stack overflow is to help with bugs, hence your question does not fit them either. You could try dreamincode.net/forums/forum/90-javascript but I will warn you your question looks a lot like homework and thus you will have a hard time getting an answer at most sites. You can always open a browser and run the code you have and that will answer your first two questions. And a hint for 3, Its valid because outside(3) returns a function that is called with the second (5) Its 2 calls, not one call with 2 args – Blindman67 Jun 27 '18 at 9:57
• @Blindman67 Thank you very much for answering. Oh my god. I'm 27 years old and I'm not working on homework. If I add my personal profile details as Engineer, will people help here. I seem to keep facing the issue of posting correct question in correct part of StackExchange. – sofs1 Jun 27 '18 at 22:52

1. outside(3) returns a function. Like you said, it returns a function takes one argument n and returns n + 3. Mathematically, this might be written as f(n) = n + 3.
2. fn_inside is a binding made to the return value of outside(3), which we know, from part 1, is a function. So when we call fn_inside(3), it's like plugging in 3 for n: f(3) = 3 + 3 = 6. The return value is simply a number: 6.
3. outside(3)(5) is equivalent to (outside(3))(5) and recall that outside(3) is a function that takes one argument.