DEV Community


Posted on

function of function & closure

A "function within a function" and a "closure" are related concepts, but they have distinct differences:

  1. Function within a Function:

    • This refers to a function that is defined inside another function.
    • The inner function can access variables and parameters of the outer function, but it does not necessarily create a closure.
    • The inner function can be invoked only within the scope of the outer function.
    • Once the outer function finishes executing, the inner function's access to the outer variables may be lost.
  2. Closure:

    • A closure is created when an inner function "closes over" its surrounding lexical (scope) environment, capturing and retaining access to variables even after the outer function has finished executing.
    • Closures allow the inner function to access and "remember" the variables of its outer function, even outside the outer function's scope.
    • Closures are not limited to being invoked only within the scope of the outer function; they can be passed around and invoked from different places, preserving access to the captured variables.

every closure is an example of a function within a function, but not every function within a function creates a closure. A closure specifically occurs when an inner function maintains access to its enclosing scope, allowing it to access variables from the outer function even after that function has completed its execution.

Top comments (2)

jonrandy profile image
Jon Randy πŸŽ–οΈ

A closure is NOT a function. EVERY function has an associated closure, regardless of whether it was created inside another function.

sharonlewis profile image
Sharon Lewis


My lost funds, totaling about $180,000, were discovered and located. I didn't know I would be able to get my trading money back after having it liquidated by a broker who for three months wouldn't let me access my trading account. I'm quite pleased that I was able to get my money returned without any problems. Wizard Larry Software Specialist I want to thank Larry Recovery, a technician who established himself as a reputable qualified binary options recovery specialist. If you have money in your account that you want to remove, your broker manager advises that you first make further deposits. Please use the following information to contact Software Specialist Wizard Larry Recovery.

Email: (

(WhatsApp: +1 (205) 319-6886 )


Image description