How To Use Closure Using JavaScript?


Share:
Published By: Admin, Published On: Nov 18, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use closure example.We will check how to use closure. This is a short guide on use closure in javascript. Here you will learn how to use javascript closure. Let's get started with how to use closure in javascript.

Here i will give you many example how to use closure using javascript.

Example : 1
<!DOCTYPE html>  
<html>  
    <head> 
        <title>How To Use Closure Using JavaScript? - MyWebtuts.com</title> 
    </head>  
    <body>  
        <h3>How To Use Closure Using JavaScript? - MyWebtuts.com</h3>  
        <script>  
            function a1()  
            {  
                var b = 8;  
                function innera2()
                {  
                    return b;  
                }  
                return innera2;  
            }  
            var output = a1();  
            document.write(output());  
            document.write(" ");  
            document.write(output());  
        </script>  
    </body>  
</html>  
Output :
8 8
Example : 2
<!DOCTYPE html>  
<html>  
    <head> 
        <title>How To Use Closure Using JavaScript? - MyWebtuts.com</title> 
    </head>  
    <body>  
        <h3>How To Use Closure Using JavaScript? - MyWebtuts.com</h3>
        <script>  
            function a1(b1)  
            {  
                function innerfun(b2){  
                    return b1*b2;  
                }  
                return innerfun;  
            }  
            var otp = a1(7);  
            document.write(otp(2));  
            document.write(" ");  
            document.write(otp(4));  
        </script>  
    </body>  
</html>  
Output :
14 28

It will help you...

Share: