1. Home
  2. Computing & Technology
  3. JavaScript

JavaScript Functions
2. How to Call a Function

By , About.com Guide

With the function defined, the next step is to call it. There are three ways that we can refer to a function compared to the two ways that other variables can be referenced.

We can assign the value of the function to a different variable. This is done in exactly the same way as we would with other variables


var yourFunction = myFunction;

We can test if the function exists. As with any other variable we can test the value that it contains (assuming it exists) by using an if statement. Unlike Boolean, number, and text string variables each of which have one value that returns false, a function will always be converted to the Boolean value true if it exists and so we can test if a particular function is actually defined using


if (myFunction) {. . .

We can run the function. This is something that you can't do with other variables. To run the code contained within a function we use


myFunction();

[em]Note that except where you are defining a function (where the reserved word function appears), the use of parentheses after the variable name indicates that the actual code contained in the function is to be run.[/em]

This tutorial first appeared on www.felgall.com and is reproduced here with the permission of the author.

Explore JavaScript
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Learn Modern JavaScript
  6. 3. Functions
  7. JavaScript Functions - How to Call a Function>

©2009 About.com, a part of The New York Times Company.

All rights reserved.