spacer

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

home / programming / javascript / diaries / 4

[next]

Ad Copywriter
Aquent
US-VA-Richmond

Justtechjobs.com Post A Job | Post A Resume
Developer News
Get Ready for Microsoft's 'Oslo' Modeling Tool
Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'

The JavaScript Diaries: Part 4

  1. Introduction
  2. Data Types & Variables
  3. Operators
  4. Functions
  5. Conditional Statements and Loops
  6. Objects
  7. Browser-Based Objects
  8. Window Methods
  9. Window Event Handlers
  10. Navigator, Screen, History and Location Objects
  11. Arrays: Part 1 - Introduction
  12. Arrays: Part 2 - Multiple Array Types
  13. Arrays: Part 3 - Array Properties and Methods
  14. The Math Object
  15. The Date Object

In this section of the JavaScript Diaries, we'll look at JavaScript functions. These help us to write more intricate programs, allowing us to accomplish more.

If you have any questions or suggestions, feel free to Remember, we're in this together. Also, be sure to visit the JavaScript forums. Looking over some of the posts and analyzing the different aspects of writing scripts helps to gain a better understanding, especially as you gain more knowledge of the language. Now, on to our next lesson ...

JavaScript Functions

A function is a set of statements used to perform a specific task. Whenever a function is needed, it's "called" from somewhere within the script or from within the page itself. Functions organize scripts into different sections — you can think of them as "subscripts" or "subroutines" — to be used within the overall script. Just as a variable is a "data container," a function is a "code container." It's actually a "self-contained process." When you create a function, you are in fact, creating a new JavaScript command that can be called from anywhere within the script.

Writing Functions

Like a variable, a function must be declared first in order to use it. This is done by using the reserved word function, followed by the name of the function and a pair of parentheses, for example function giveName(). (Do not end the line with a semi-colon.) This is then followed by a pair of curly brackets ( "{ }" ) which contain the actual code that will be executed when the function is called. (Do not put a semi-colon after the closing curly bracket either; each line within the curly brackets will end with a semi-colon.) The formatting of the curly brackets is really a matter of preference. The two formats used most are shown below (we'll be using the first one):

  function giveName() {
    code goes here;
    code goes here;
  }


  function giveName()
  {
    code goes here;
    code goes here;
  }

Naming Rules

Function names follow the same rules as variables:

  • Names can only begin with a letter or underscore ( "_" ) and cannot contain any spaces.
  • Names are case sensitive: newName, NewName, and NEWname are all different names to the JavaScript interpreter.
  • When a function is called, it must have the same case usage as the original declaration.
  • Reserved words cannot be used to name functions.

Calling a Function

Until it is "called," a function merely stores code. To call a function, list the function name followed by a pair of parentheses and a semi colon, such as giveName();.

A function call can be placed anywhere on the Web page — in the <head> section within the script or in the <body> section. It can also be placed in an external file. If it is called within the <body> tag, it must be enclosed within script tags just like a regular script; otherwise the JavaScript interpreter will see it as regular text and not execute it. A function can even be called from within another function, but the code must first be loaded into memory in order for it to be used.

Calling a function from within the <head>:

<script type="text/javascript">
<!--
  function readyMessage()
  {
    alert("Are you ready for this?");
  }
  readyMessage();
//-->
</script>

Calling a function from within the <body>:

<script type="text/javascript">
<!--
  readyMessage();
//-->
</script>

It's important to remember that no matter where the function itself is actually located, it will not execute until it is called. The function must be loaded into memory in order for it to be called so it must be located before the actual calling statement, even if it is run from within the function itself. It's like a software program on your computer. You already have the code loaded on your hard drive but in order for it to run it has to be "called," otherwise it just sits there and doesn't do anything.

home / programming / javascript / diaries / 4

[next]



JupiterOnlineMedia

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

Solutions
Whitepapers and eBooks
Intel Article: Using Power & Display Context in the Intel Mobile Platform SDK
Internet.com eBook: Real Life Rails
IBM SCA Center Article: Simplifying Composite Applications with Service Component Architecture
Intel PDF: Quad-Core Impacts More Than the Data Center
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Intel PDF: Analysis of Early Testing of Intel vPro in Large IT Departments
Internet.com eBook: Best Practices for Developing a Web Site
Intel PDF: IT Agility through Automated, Policy-based Virtual Infrastructure
IBM CIO Whitepaper: The New Information Agenda. Do You Have One?
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
IBM Whitepaper: How are other CIOs driving growth?
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Actuate Download: Free Visual Report Development Tool
Red Gate Download: SQL Backup Pro
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
IBM SCA Download: Start Building SCA Applications Today
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Anatomy of an Ajax Application · Popular JavaScript Framework Libraries: An Overview · Controllers: Programming Application Logic - Part 2
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags

Created: May 27, 2005

URL: