Javascript Functions

What is a function?
How to use javascript functions?

Explanation

A function is nothing but a group or block of statements doing a specific task.
Syntax:
function name()
{
// set of statements that will be executed
}

A function has to be defined in the above syntax.
The name "function" followed by the name we choose for the function and then open and close brackets. The statements that will do the specific operation are then group together under this name using braces.
A function may or may not return a value. A function may or may not have parameters value.
Invoking a function:
The statements inside the function will not be executed automatically. We have to invoke or call the function to execute the statements. Just calling the name of the function will invoke the function. i.e. if we write a function with the name "test" calling it as "test();" will invoke the function.

Example:


<script language="javascript">
function test()
{
document.write(" ---- This is a test function ---- ");
}
test();
</script>

Result:




Ask Questions

Ask Question