مثال
بازگشت به صفحه اصلی
بازگشت به صفحه مقاله
دو ستونی
دو سطری
اجرای کد RUN »
<!DOCTYPE html> <html> <body> <h2>JavaScript Function Closures</h2> <p>Counting with a local variable.</p> <p id="demo"></p> <script> // Initiate counter var counter = 0; // Function to increment counter function add() { var counter; counter += 1; } // Call add() 3 times add(); add(); add(); // The result is not 3 because you mix up the globaland local counter document.getElementById("demo").innerHTML = "The counter is: " + counter; </script> </body> </html>