spacer

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

home / programming / javascript / diaries / 4

[previous]

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

Return Statements

If you create a function and call it once, that works well, but what if you want the function to carry out a task that can by reused within the main script multiple times? JavaScript uses a "return" statement for this very situation. The return statement must be on the last line of the function, before the closing bracket. This is important as the execution of the function stops once a return statement is encountered; anything after that is not processed.

Here is what a function would look like with the return statement:

function getAddedText() {
  var textPart1="This is ";
  var textPart2="fun";
  var addedText=textPart1+textPart2;
  return addedText;
}
var alertText=getAddedText();
alert(alertText);

Let's take a look at what is happening here. Remember, while the function is not executed until it is called, the code should be in memory prior to the function call to avoid any possible errors.

  • This script actually begins on the seventh line with the declaration of the variable alertText. It is initialized with the value of the function getAddedText.
  • At the same time, a function call is initiated and the function getAddedText is executed.
    • Two local variables are declared, textPart1 and textPart2, and initialized with the values of "This is " and "fun," respectively.
    • A third variable, addedText, is declared and initialized with the values of the first two variables. The results are joined together ("concatenated").
    • A return statement is then issued, sending the value of the variable addedText back to the place in the script where the call was first made.
  • An alert window is then displayed with the result of the variable alertText. Remember, we had previously assigned the results of the function getAddedText to the variable alertText.

It's gets a little complicated but if you break it down into segments — which is what the JavaScript interpreter does — it's fairly easy to follow. Without a return statement, the function is only used when a function call is issued and then it ends. With a return statement, the results can be incorporated within the overall script and can be used several times.

Using the return statement in the function also allows the results to be further manipulated outside the function, within the overall script. Let's use the above example and make a few additions:

function getAddedText() {
  var textPart1="This is ";
  var textPart2="fun";
  var addedText=textPart1+textPart2;
  return addedText;
}
var moreText=" isn't it?";
var alertText=getAddedText()+moreText;
alert(alertText);

As I stated earlier, it's important to remember that the return statement is placed in the last line of the function, before the closing curly bracket as it stops all further processing of the function code. For example, in the following code, the function stops calculating after the return statement. The alert box will return a result of "undefined" as the function did not complete its 'program,' Run it and see how it works.

function getAddedText() {
  var textPart1="This is ";
  var textPart2="fun";
  return addedText;
  var addedText=textPart1+textPart2;
}
var alertText=getAddedText();
alert(alertText);

Return statements are very valuable in validating user input, as in forms. They also have many other uses.

Conclusion

That will wrap it up for our study of JavaScript functions. If you're like me, your head is probably still spinning a bit. Next time we'll look at conditional statements and loops. Remember, if you have any questions, feel free to

To Part 2 Continue on to "The JavaScript Diaries: Part 5"

home / programming / javascript / diaries / 4

[previous]

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
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
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: