JavaScript
  Home arrow JavaScript arrow Scope of Variables and Arguments in Nested...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Scope of Variables and Arguments in Nested JavaScript Functions
By: Chrysanthus Forcha
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-10-15

    Table of Contents:
  • Scope of Variables and Arguments in Nested JavaScript Functions
  • Argument Scope within Nested Functions
  • Function Call, Argument and Variable Scope Summary
  • Passing Arguments to Inner Functions from Outside

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Scope of Variables and Arguments in Nested JavaScript Functions


    (Page 1 of 4 )

    In this part of the series we shall look at the scope of variables and arguments in nested functions. We shall see whether the outer function can use the variables and arguments of the inner functions and vice versa. We shall also see how many levels downward or upward this can be done.

    Variable Scope within Nested Functions

    Consider the following code segment, which is the modified form of the example I mentioned in the previous part:

    function fnName1()

    {

    alert(variableA2);

    function fnName2(arg2)

    {

    var variableA2 = "I am variable 2";

    function fnName3()

    {

    var variableA3 = "I am variable 3";

    }

    }

    }


    The nested function fnName2() has the following variable declared:

    var variableA2 = "I am variable 2";

    The nested function fnName3() has the following variable declared:

    var variableA3 = "I am variable 3";

    The outermost function has an alert statement, which would attempt to display the value of the variable in the middle function. Now if you call the outermost function in your browser, nothing will happen, or you will see an error message in the task bar. If you replace the alert statement with “alert(variableA3)” with the hope of displaying the variable of the innermost nested function, you would still have the same result (nothing happens).

    The truth is, independent of the level of the inner function, an outer function cannot use (see) the variable of the inner function. We shall now look at the reverse case.

    Consider the following code:

    <html>

    <head>

    <script type="text/javascript">

    function fnName1()

    {

    var variableA1 = "I am variable 1";

    fnName2(); //call fnName2()

    function fnName2()

    {

    alert("Called from middle, " + variableA1);

    function fnName3()

    {

    //nothing here

    }

    }

    }

    </script>

    </head>

    <body>

    <button type="button" onclick="fnName1()">Call fnName1 Function</button>

    </body>

    </html>

    A variable has been declared only in the outermost function (outside the nested functions). The variable's name is variableA1, the value is “I am variable 1.” This outermost function calls the middle function. The middle function has an alert statement, which would display the string "Called from middle, " and the value of the variable of the outermost function. If you try the code (click the button), the alert box will display:

    "Called from middle, I am variable 1"

    Consider the following code:

    <html>

    <head>

    <script type="text/javascript">

    function fnName1()

    {

    var variableA1 = "I am variable 1";

    fnName2(); //call fnName2()

    function fnName2()

    {

    fnName3(); //call fnName3()

    function fnName3()

    {

    alert("Called from innermost, " + variableA1);

    }

    }

    }

    </script>

    </head>

    <body>

    <button type="button" onclick="fnName1()">Call fnName1 Function</button>

    </body>

    </html>

    Again, a variable has been declared only in the outermost function (outside the nested functions). The variable's name is variableA1, and the value is “I am variable 1.” Here the outermost function calls the middle function, which calls the innermost function. The innermost function has an alert statement, which would display the string "Called from innermost, " and the value of the variable of the outermost function. If you try the code (click the button), the alert box will display:

    "Called from innermost, I am variable 1"

    Independent of the level of the outer function, an inner function can use the variables of the outer function. When we say a function can use variables, we are referring to the statements in the function.

    More JavaScript Articles
    More By Chrysanthus Forcha


     

    JAVASCRIPT ARTICLES

    - Comparing Fields and Customizing Error Messa...
    - Checking Numbers and File Extensions with jQ...
    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek