DEV Community

Karthikeyan
Karthikeyan

Posted on

3 2 1 1 2

One Byte Explainer - Closures

Hello 👋

Let's start with Closures

One-Byte Explainer:

Imagine a magic box! Put your favourite toy inside, close it, & give it to a friend. Even though you can't see it, your friend can open it and play!

Demystifying JS: Closures in action

Closures are like backpack the inner-functions carry when they exit the outerfunctions , even after the outer function's execution is destroyed.

  1. Function Inside a Function: You have a function inside another function.

  2. Access to Outer Variables: The inner function can access variables from the outer function.

  3. Retain Variables: Even after the outer function finishes, the inner function keeps the outer variables.

Example :


function outer() {
    let outerscope = "I am outer scope";

    function inner() { 
        let innerScope = "I am in inner scope";
        console.log(`${outerscope} and ${innerScope}`);
    }

    return inner;
}

const calling = outer();
calling(); // Output: "I am outer scope and I am in inner scope"

Enter fullscreen mode Exit fullscreen mode

Thank you for reading , See you in the next blog

final

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay