spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / diaries / 4

[previous] [next]

Senior Consultant/Information Security - permanent position (TX)
Next Step Systems
US-TX-Irving

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

The JavaScript Diaries: Part 4

Global and Local Variables

As you have seen, variables can be used within functions, but not all variables used within a function are the same. There are two types of variables used within a function: global and local.

Global Variables

These are variables declared outside the function, within the overall script. If the value of a global variable is reassigned (changed) within a function, it will be reassigned throughout the entire script. This is because it "belongs" to the script itself. When a global variable is sent to a function, just the variable name is used (don't use the reserved word var). All of our examples so far in this installment have been local since all used the var reserved word.

If we create the following script, and declare the variables using the reserved word var, it will write "I like the Delta blues" on the page:

<script type="text/javascript">
<!--
  var place="Delta";
  var type="blues";
  document.write("I like the " + place + " " + type);
//-->
</script>

If we take the same script, add a function and reuse the global variables above within the function without using the reserved word var, it will create a bit of a problem, as shown below. Put this in the <head> section:

<script type="text/javascript">
<!--
  var place="Delta";
  var type="blues";
  function music() {
    place="New Orleans";
    type="jazz"
    document.write("I like " + place + " " + type);
  }
//-->
</script>

Then, add this in the <body> section:

<script type="text/javascript">
<!--
  music();
  document.write(" but I really like " + place + " " + type);
//-->
</script>

Here is what will happen:

  • The variables place and type will be declared and initialized with the values of "Delta" and "music," respectively.
  • Next, the function music() is declared.
  • The next event is the function call, music();. (Remember, the function itself is merely loaded into memory. It's not executed until it's called.)
  • The variables place and type, which are located within the function music(), are then reassigned new values, "New Orleans" and "jazz," respectively. They are reassigned because they are global variables.
  • The script will then write to the Web page, "I like New Orleans and jazz but I really like New Orleans and jazz." The problem is that you reassigned the global variables place and type, which changed their value throughout the entire script.

Local Variables

These variables are declared within the function (hence, they are "local"). In order to be declared locally they must be declared using the reserved word var. You can even use the same name as a global variable but its value will only be used within the function. If we use our previous example and add the var reserved word before the variables within the function, we will get the anticipated result. Go ahead and try both of them yourself.

<script type="text/javascript">
<!--
  var place="Delta";
  var type="blues";
  function music()
  {
    var place="New Orleans";
    var type="jazz"
    document.write("I like " + place + " " + type);
  }
//-->
</script>
<script type="text/javascript">
<!--
  music();
  document.write(" but I really like the " + place + " " + type);
//-->
</script>

As far as the JavaScript interpreter sees it, the variables place and type declared at the very beginning of the script are completely different than the variables place and type declared locally within the function. Be sure to remember that, without using the var reserved word within the function when declaring variables, you will be assigning a new value to an existing global variable.

home / programming / javascript / diaries / 4

[previous] [next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: May 27, 2005

URL: