Ads Top

What is closure in javascript?

 Javascript Closures:

Closure means scope, accessing boundaries of variables. A closure function can access all variables with his boundary means inner function access inner side variable also access our outer side scope variable.

 

Closures example in javascript:

function closure(){

            var i = 50;

            console.log(`Outer function: ${i}`);

            function innerClouser () {

                var j = 70;

                console.log(`Inner function: ${j} +++ Outer variable: ${i}` );

            }

            innerClouser();

            console.log(`Outer function: ${i} ++++++++++ Inner function: ${j}`);

            // j is not defined because we are accessing inner variable in outside so it's not accessible.

        }  

        closure();




No comments:

Note: Only a member of this blog may post a comment.

Copyright Reserved to Anything Learn. Powered by Blogger.