AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

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:

Example:
function functionName(Parameter1, Parameter2, ..)
{
// code to be executed
}


Consider the following example, code function

Example: a simple code, change parameters
<script>
function my_Function(x1, x2)
{
return x1 * x2;
}
document.getElementById("my_demo").innerHTML = my_Function(5, 11);
</script>
Note: A Function is more o less the same as a Procedure or a Subroutine, in other programming languages.


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

Example:

<!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) {
return y * z;
}
</script>

</body>
</html>
Output:
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.

Example: syntax
<script>
function name(parameters)
{
...body...
}
</script>



Examples, Our new function can be called by its name: showMessage()

Call showMessage() executes the code of the function. Here we will see the message three times.
Example:

<!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>
Output:
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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.