JavaScript Function
JavaScript Tutorial » JavaScript Function
JavaScript function is a block of code that perform specific tasks and can be kept and maintained separately from main program.
A JavaScript function is executed when "something" invokes it (calls it).
Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
Why we need Functions?
Because you can define the code once, and use it how many times need it.
You can use the same code many times with different arguments, to produce different results.Syntax:
{
// code to be executed
}
Consider the following example, code function
<script>
function my_Function(x1, x2)
{
return x1 * x2;
}
document.getElementById("my_demo").innerHTML = my_Function(5, 11);
</script>
Calling Functions - example
After defining a function, the next step is to call them to make use of the function.
We can call a function by using the function name separated by the value of parameters enclosed between parenthesis and a semicolon at the end.
The example below shows how an already created function can be called<!DOCTYPE html>
<html>
<body><h3>JavaScript Functions</h3>
<p>This example below will calls a function what performs a multiplication and will return value:</p>
<p id="my_demo"></p>
<script>
var x = my_Function(11, 7);
document.getElementById("my_demo").innerHTML = x;function my_Function(y, z) {
</body>
return y * z;
}
</script>
</html>
JavaScript Functions
This example below will calls a function what performs a multiplication and will return value:
77
Function Declaration - example
To create a function we can use a function declaration.
function name(parameters)
{
...body...
}
</script>
Examples, Our new function can be called by its name: showMessage()
<!DOCTYPE html>
<html>
<body><h2>JavaScript Functions</h2>
<p>Our new function can be called by its name: showMessage()</p>
<script>
function show_Message() {
alert( 'Hi, This is my JS function' );
}show_Message();
show_Message();
show_Message();</script>
</body>
</html>
Hi, This is my JS function
Hi, This is my JS function
Hi, This is my JS function
javascript function parameters, declaration, return, in function, constructor, arguments, call, variable, exists, as parameter
javascript function parameters, declaration, return, in function, constructor, arguments, call, variable, exists, as parameter
JavaScript Function - javascript tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.