JavaScript Tutorial JavaScript References

JavaScript - Functions



A function is a block of statements which executes only when it is called somewhere in the program. A function provides re-usability of same code for different inputs, hence saves time and resources. JavaScript has many built-in functions and one of the common function is String() which converts an object's value to a string. Users can also create their own function which is also termed as user-defined functions.

Create Function

In JavaScript, creating a function starts with function keyword followed by function's name and parenthesis containing function's parameter(s), if it has any. At last, it contains block of statements which is also called body of the function.

Call Function

After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any and semicolon ;.

Syntax

//Defining function
function function_name(parameters) {
  statements;
}

//Calling function
function_name(parameters);

Example: A function with no parameter

In the example below, a function MyFunction is created to write "Hello World" in the document when called.

function MyFunction(){
  document.write ("Hello World!"); 
}

MyFunction();

The output of the above code will be:

Hello World!

Parameter

A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. The value of a parameter can be further used by the function to achieve desired result.

Example: A function with parameters

In the example below, the function called MyFunction is created which requires two arguments and write the sum of these arguments in the document. Please note that the function returns nothing here.

function MyFunction(a, b){
  document.write ("The sum is ", a + b); 
}

MyFunction(10, 15);

The output of the above code will be:

The sum is 25

Function to Return Values

A function can be used to return values. To achieve this, user must have to use return keyword in the body of the function.

Example: A function with return value

In the example below, the function called MyFunction takes two arguments and returns the sum of these arguments.

function MyFunction(a, b){
  return a + b; 
}

var txt = "The sum is " + MyFunction(10, 15);

The output (value of txt) after running above script will be:

The sum is 25

Default Parameter Value

Default value can be assigned to a parameter at the time of creating function. When the function is called without parameter then it uses default value.

Example:

In the example below, default parameter value is used to perform sum operation on two, three and four parameters using same function called MyFunction. Here, default value is set while defining the function.

function MyFunction(a, b, c=0, d=0){
  return a + b + c + d; 
}

var txt;
txt = "MyFunction(2, 4): " + MyFunction(2, 4) + "<br>";
txt = txt + "MyFunction(2, 4, 6): " + MyFunction(2, 4, 6) + "<br>";
txt = txt + "MyFunction(2, 4, 6, 8): " + MyFunction(2, 4, 6, 8) + "<br>";

The output (value of txt) after running above script will be:

MyFunction(2, 4): 6
MyFunction(2, 4, 6): 12
MyFunction(2, 4, 6, 8): 20

Local Variables

A variable defined inside a JavaScript function is a local variable and can only be accessed within the function.

Example

In the example below, when the typeof operator is used with a variable which is defined inside a function, it returns undefined.

function MyFunction(){
 var MyNum = 10
}

var txt;
MyFunction();
txt = "typeof MyNum : " + typeof MyNum;

The output (value of txt) after running above script will be:

typeof MyNum : undefined




5