SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Parameter Names

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Port Sunlight
    Posts
    815
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parameter Names

    Hi,

    Thought I would go back to basics and learn the foundation of this language.

    Doing ok, but I need clarification.

    Why do these different functions, still behave the same?

    Code JavaScript:
    function doSomething(name) { 
         alert('Hello ' + name); 
    }
     
         var name = 'Nick';
         doSomething(name);

    Code JavaScript:
    function doSomething(firstName) { 
         alert('Hello ' + firstName);
    }
     
         var name = 'Nick';
         doSomething(name);

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you pass in as a parameter has a local scope: it is a local variable. In your example above, the functions are, in essence (except nominally), identical.

    What you may be getting confused with is the fact that you did:
    doSomething(name);
    While the first example may seem to make more sense, the second example works because you passed in the variable 'name' into the function's local variable (parameter). The 'name' variable that you passed it automatically becomes 'firstName', local to the function.

    Hopefully that was clear enough.

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Port Sunlight
    Posts
    815
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, just wanted clarification.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •