Using Functions:

The purpose of using a function is to take advantage of reusable code-block that will be executed by an event, or when the function is called.It's purpose is to perform a single task or a series of task.

As mentioned earlier, functions

  • are triggered by events,
  • may be called from other functions, and
  • reside in the head section of an HTML Document.

Function syntax


Functions are declared using the reserved word function followed by the name of the function, and an opening and closing bracket used for parameters. The code for these function begin after the open curly bracket and end with a closing curly bracket.






For example ( throughout this document I use a function called TRYIT. This function is created using JavaScript in an external script file. I use the click method to trigger this function that allows the actors to try html code).

Functions can be called:


function changecolor()

JavaScript Code here


The reserve word "function " is used to declare the function changecolor

Arrow pointing to the reserve word function

This open curly bracket indicate the beginning of the code.

Arrow pointing to the open curly bracket
the arrow pointing to  the location for the closing curly  bracket

This closing curly bracket indicate the end of the function.

Place the JavaScript code between the opening & closing brackets to execute the code.

arrow pointing to the location of javascript code




J s - f u n c t i o n s