W3Schools

home HOME

JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS If...Else
JS Switch
JS Operators
JS Popup Boxes
JS Functions
JS For Loop
JS While Loop
JS Break Loops
JS For...In
JS Events
JS Try...Catch
JS Throw
JS onerror
JS Special Text
JS Guidelines

JS Objects
JS Objects Intro
JS String
JS Date
JS Array
JS Boolean
JS Math
JS HTML DOM

JS Advanced
JS Browser
JS Cookies
JS Validation
JS Animation
JS Image Maps
JS Timing
JS Create Object
JS Summary

Examples/Quiz
JS Examples
JS Object Examples
JS DOM Examples
JS Quiz

JS References
JS Objects
JS HTML DOM

Selected Reading
Web Statistics
Web Glossary
Web Hosting
Web Quality

W3Schools Forum

Helping W3Schools

pixels

JavaScript Functions

previous next

A function is a reusable code-block that will be executed by an event, or when the function is called.


Examples

Function
How to call a function.

Function with arguments
How to pass a variable to a function, and use the variable in the function.

Function with arguments 2
How to pass variables to a function, and use these variables in the function.

Function that returns a value
How to let the function return a value.

A function with arguments, that returns a value
How to let the function find the product of 2 arguments and return the result.


JavaScript Functions

To keep the browser from executing a script as soon as the page is loaded, you can write your script as a function.

A function contains some code that will be executed only by an event or by a call to that function.

You may call a function from anywhere within the page (or even from other pages if the function is embedded in an external .js file).

Functions are defined at the beginning of a page, in the <head> section.

Example

<html>
<head>
<script type="text/javascript">
function displaymessage()
{
alert("Hello World!")
}
</script>
</head>
<body>
<form>
<input type="button" value="Click me!"
onclick="displaymessage()" >
</form>
</body>
</html>

If the line: alert("Hello world!!"), in the example above had not been written within a function, it would have been executed as soon as the line was loaded. Now, the script is not executed before the user hits the button. We have added an onClick event to the button that will execute the function displaymessage() when the button is clicked.

You will learn more about JavaScript events in the JS Events chapter.


How to Define a Function

The syntax for creating a function is:

function functionname(var1,var2,...,varX)
{
some code
}

var1, var2, etc are variables or values passed into the function. The { and the } defines the start and end of the function.

Note: A function with no parameters must include the parentheses () after the function name:

function functionname()
{
some code
}

Note: Do not forget about the importance of capitals in JavaScript! The word function must be written in lowercase letters, otherwise a JavaScript error occurs! Also note that you must call a function with the exact same capitals as in the function name.


The return Statement

The return statement is used to specify the value that is returned from the function.

So, functions that are going to return a value must use the return statement.

Example

The function below should return the product of two numbers (a and b):

function prod(a,b)
{
x=a*b
return x
}

When you call the function above, you must pass along two parameters:

product=prod(2,3)

The returned value from the prod() function is 6, and it will be stored in the variable called product.


previous next


What do you want your Web site to do?

Ektron

What do you want your Web site to do? Ektron CMS400.NET lets you do everything you need to do on the Web and do everything you want to do.

Ektron Ektron CMS400.NET gives you all the features you would expect from an enterprise CMS, but also high-demand functionality like real-time Web site analytics, community building, blogging, personalization, and online calendars. Ektron CMS400.NET bridges your Web site gaps.

Use Ektron CMS400.NET for:

  • Content & Document Management - Author/edit content, manage navigation, menus, audit trails, workflow, approvals
  • Personalzation & Portal Support - Allow visitors to create their own view of your web page
  • 508 & W3C Compliance Checker - Ensure compliance with scheduled or on-demand compliance reports
  • Memberships - Build online communties with blogs and forums, enable email web alerts, build polls and surveys
  • Web Site Analytics - track visitors, report on most requested pages, see where they came from and where they are going
  • HTML Form Engine - Create a two-way dialog with visitors using our wizard-like HTML form-building engine with validation and database support
  • Multilingual Support - Integrated translation tools help you reach multiple audiences by speaking their language
  • Online Calendars - Build and manage Web-based calendars and enable users to customize a view based on what’s important to them

See why there are 15,000+ Ektron integrations worldwide.

Click here to learn more, then request a live online demo or download a FREE TRIAL




diploma   

Get Your Diploma!

W3Schools' Online Certification Program is the perfect solution for busy professionals who need to balance work, family, and career building.

The HTML Certificate is for developers who want to document their knowledge of HTML, XHTML, and CSS.

The ASP Certificate is for developers who want to document their knowledge of ASP, SQL, and ADO.


Jump to: Top of Page or HOME or Printer Friendly Printer friendly page

W3Schools provides material for training only. We do not warrant the correctness of its contents. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2007 by Refsnes Data. All Rights Reserved.

Validate Validate W3C-WAI level A conformance icon W3Schools was converted to XHTML in December 1999
SITE SEARCH
 
About W3Schools
W3Schools Forum
ASPHits
Active Server Pages Resource Web Site
WEB HOSTING
$15 Domain Name
Registration
Save $20 / year!
UK Domain Names
UK Web Hosting

Alojamiento Web
Buy UK Domain Names
Register Domain Names
Cheap Domain Names
Cheap Web Hosting
Best Web Hosting
Bulk Domain Names
UK Web Hosting
PHP MySQL Hosting
Web Hosting UK
Top 10 Web Hosting
Web Hosting Providers
ASP.NET Web Hosting
3 Months Free!
Programming Forums
Windows Web Hosting
WEB BUILDING
XML Website Tools
Website Templates
Flash Templates
Website Builder
Internet Business Opportunity
Custom Programming
BEST Flash Templates
Web Design Forum
Programming Forum
FREE Trial or Demo
Web Content Manager
Forms,Web Alerts,RSS
HTML Exam
SHOPPING
UK Wholesalers
UK Wholesale
ONLINE DEGREE
Advance Your Career
with Drexel University
Bachelor's & Master's
Tech Degrees ONLINE

pixels