JavaScript Functions

How to Call a Function

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


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.

go to previous section go to next section 


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow