Sign Up

Talk Programming , Career, Mental Health, Talk Personal Finance ❤️‍ Post a query and receive responses ✅

Have an account? Sign In


Have an account? Sign In Now

Sign In

Post a query and receive responses. Ask anything, Ask Mitra ❤️‍

Sign Up Here


Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.


Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.


Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Ask Mitra

Ask Mitra Logo Ask Mitra Logo

Ask Mitra Navigation

  • Home
  • About Us
  • Find Jobs
  • Blogs
    • Add New Blog !
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Ask a Question
  • Home
  • All Categories
  • Find Jobs
  • Points and Swags
    • Badges and Points
    • Ask Mitra Swags
  • Blog
    • Add New Blog !
  • FAQs
  • Community Members 💓
  • About Us
  • Contact Us
Home| Questions|Q 62997
Next
In Process
SabitaOjha
SabitaOjhaBeginner
Asked: July 3, 20222022-07-03T15:42:41+05:45 2022-07-03T15:42:41+05:45In: Web Development

Closure in Javascript

What is closure in Javascript? What is the need of closure in js? Where is it good to use practically? I went through some articles but could not able to understand the actual use of closure in js. Explain with examples so that i will understand the concept of closure in a better way.

#closure #javascript
  • 3
  • 5 5 Answers
  • 135 Views
  • 0 Followers
  • 0
Share
  • Facebook

    Related Questions

    • How to check if the bat/ball touches the 4 sides of the box?(Javascript)
    • Input Direction confusion in snake game JavaScript?
    • which tech jobs have higest demand but least supply in Kathamandu, Nepal?
    • What does ReactJS intern do in a internship programme?
    • How to implement mapbox in nextjs in latest version of react-map-gl

    You must login to add an answer.


    Forgot Password?

    Need An Account, Sign Up Here

    5 Answers

    • Voted
    • Oldest
    • Recent
    • Random
    1. THE BIG OCEAN Explainer
      2022-07-04T12:59:02+05:45Added an answer on July 4, 2022 at 12:59 pm

      A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives you access to an outer function’s scope from an inner function.

      Look at the example below:

      function makeFunc() {
        const name = 'THE BIG OCEAN';
        function displayName() {
          console.log(name);
        }
        return displayName;
      }
      
      const myFunc = makeFunc();
      myFunc();

      Closures are useful because they let you associate data (the lexical environment) with a function that operates on that data. This has obvious parallels to object-oriented programming, where objects allow you to associate data (the object’s properties) with one or more methods.

      Links that can help you to understand:

      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

      • 1
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    2. Reymes Giree Beginner
      2022-07-04T17:21:39+05:45Added an answer on July 4, 2022 at 5:21 pm

      Closure is similar to lexical environment/scope… Outer/parent function ko variables chai inner/child function le access garna paunxa…. And also … inner function lai return gari sake paxi pani… Memory maa /closure maa tyo variable chai rahi rakhxa…note:- return gari sake paxi… Outer Function ko environment sakinxa…. But still inner function maa chai tyo variable access garna sakxau outer function ko…. Tesai lai closure vanxa….

       

      Eg:- euta outerFuction create gara…tesma 2/3 ota variable banau …ani innerFuction create gara…. inner function lai direct call na gari… Return gardeu ….

      return innerFuction. ..like this

      And outerFuction vanda bairaw do ..

      let inner = outerFuction()

      console.dir(inner) gara…tesma closure vannw scope hunxa…tya outer function ko variables vetxau…. That means … Even tho you have returned inner function inside outer function… Still that inner function has access variables of that outer function…

      Hope you understood it… If anydoubt …dm me

      Practical use ko case chai yesto ho… function declaration garey paxi… Closure aafai create hunxa…. So..its a concept you should know… Always asked in interviews…
      Timle aile samma unknowingly closure chai use gari rakhe ko xau… Just know the concept about closure thats enough

      • 1
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    3. Neri Shrestha Explainer 🦋
      2022-07-05T13:16:13+05:45Added an answer on July 5, 2022 at 1:16 pm

      You would love to check this

      https://www.facebook.com/groups/itsnporg/posts/1163712074410221/

      • 1
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    4. [Deleted User]
      2022-07-03T18:24:29+05:45Added an answer on July 3, 2022 at 6:24 pm

      this might be helpful

      • 0
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
    5. Sheyshya Beginner
      2022-08-02T20:28:42+05:45Added an answer on August 2, 2022 at 8:28 pm
      This answer was edited.

      In nesting functions,  even after the outer function has completed its execution, the inner function can access the variables of the outer/parent function. For eg:

      function outer(){

      let counter=0;

      function inner(){

      counter ++;
      console.log(counter)

      }

      return inner

      }

      const count = outer()
      count()
      count()

      The output will be 1 and 2.  Here even after the outer function has been executed, the value of  Counter (this variable is defined in the outer function) is remembered in the inner function. Hope it helped.

      • 0
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp

    Sidebar

    Ask A Question

    Your Favourite Topics

    • Announcements
    • Blockchain
    • Books

        • —Non Tech Books
        • —technical books
    • Business and Entrepreneurship
    • Careers
    • College
    • Content and Blog
    • CyberSecurity
    • Design and Arts
    • Devices and Gadgets
    • Digital Marketing
    • Entertainment

        • —Movies-Series-TV
        • —Music
        • —Sports
    • Events
    • For Admins only.
    • Health

        • —Fitness
    • Money

        • —Personal Finance
        • —Stock Market
    • Operating System

        • —linux
        • —windows
    • Other
    • Paid Internship
    • Programming

        • —AI/ML
        • —APIs
        • —C & C#
        • —Django
        • —Flutter
        • —Go
        • —HTML&CSS
        • —Java
        • —JavaScript
        • —Mobile Apps Development
        • —Node Js
        • —NoSQL
        • —Php
        • —Python
        • —React JS
        • —SQL

    Stats

    • Questions 1k
    • Answers 10k
    • Best Answers 391
    • Users 2k

    Top 6 Members This Month

    Vicky

    Vicky

    • 0 Questions
    • 78 Points
    Sensei
    puppet

    puppet

    • 0 Questions
    • 23 Points
    Beginner
    wtfixair

    wtfixair

    • 0 Questions
    • 22 Points
    Beginner
    Dhanmaya

    Dhanmaya

    • 0 Questions
    • 20 Points
    Beginner
    Riyesh

    Riyesh

    • 0 Questions
    • 20 Points
    Beginner
    pranitkarki

    pranitkarki

    • 0 Questions
    • 20 Points
    Beginner

    Trending Tags

    askmitra blockchain c++ career coding College cybersecurity Django Flutter help javascript laptop laravel linux poll Programming python question react web web development wordpress
    • Popular
    • Answers
    • Rounak Agrawal

      Ask Mitra Udemy Courses Giveaway

      • 76 Answers
    • Subedi Bibek

      The Complete JavaScript Course 2022: From Zero to Expert! Udemy ...

      • 73 Answers
    • Subedi Bibek

      Mega Giveaway at askmitra.com 🔥

      • 69 Answers
    • bishwaKiran
      bishwaKiran added an answer Try getting into githubs student program, use your university/collage/school ID… January 30, 2023 at 7:44 pm
    • Sandeep Poudel
      Sandeep Poudel added an answer SOLVED: It was due to unfinished SSL certificate installation January 27, 2023 at 7:11 pm
    • Aashutosh_k
      Aashutosh_k added an answer Software piracy protection system Fingerprint voting system Stock price prediction… January 26, 2023 at 7:37 pm

    Related Questions

    • How to check if the bat/ball touches the 4 sides ...

      • 2 Answers
    • Input Direction confusion in snake game JavaScript?

      • 1 Answer
    • which tech jobs have higest demand but least supply in ...

      • 1 Answer
    • What does ReactJS intern do in a internship programme?

      • 2 Answers
    • How to implement mapbox in nextjs in latest version of ...

      • 1 Answer

    Explore

    • Home
    • Trending Category
      • Programming
      • Web Development
      • —Mobile Apps Development
      • —Stock Market
      • Business and Entrepreneurship
      • Blockchain
      • College
      • —Personal Finance
      • —HTML&CSS
      • —JavaScript
      • —Python
      • —React JS
      • —Node Js
      • —C & C#
      • Content and Blog
      • Careers
    • All Categories
      • Go to All Categories
      • Health
      • —Go
      • —Flutter
      • Digital Marketing
      • —APIs
      • Devices and Gadgets
    • Find Job and Internships
    • Points and Swags
      • Badges and Points
      • Ask Mitra Swags
    • All Blogs
      • Add New Blog !
    • FAQs
    • Community Members 💓
    • Questions
      • Un Answered
      • New Questions
      • Most Seen Questions
      • Most Answered
      • Trending Questions
    • Tags

    Footer

    Ask Mitra

    Ask Mitra

    Ask Mitra is a social question and answer engine that will assist you in forming a community and connecting with others. Talk about programming, your career, your mental health, and your personal finances. ❤️‍ Post a query and receive responses ✅

    Quick Navigation

    • Ask a Question
    • Home
    • All Categories
    • Find Jobs
    • Points and Swags
      • Badges and Points
      • Ask Mitra Swags
    • Blog
      • Add New Blog !
    • FAQs
    • Community Members 💓
    • About Us
    • Contact Us

    Legal Stuff

    • Terms of use and policy