JavaScript function and their types


This is basic tutorial about function in Javascript. Here we demonstrate the types of function and where could they be used. We are mentioning function, callback function, method and anonymous function.

This tutorial is all about functions and their types in JavaScript, here we are going to talk about following:

  • Simplest form of Javascript Function
  • Anonymous function
  • Callback function
  • Method

Function

We are starting with the simplest form of the function and even basic, the general syntax of it.


function functionName(argument-1, argument-2,..argument-n){
	/*function body*/
	...
	return value; /*optional - a function in JavaScript may or may not return value*/
}

In the code above the "function" is a keyword followed by the "name of function", then there may or may not be number of "arguments", then function body with the optional "return" statement. In the next code snippet we are giving two examples of the function, one with return statement and one without return statement.


/*this function is a simple function which on call will alert a message*/
function myFavouriteNovel(){
	alert("my favourite novel is, Harry Potter");
}

/*this function takes a name from user and return a modified message*/
function sayHello(name){
	return "Dear "+name", How are you?";
}

/*invoking the functions*/
myFavouriteNovel();

var msg = sayHello("James Bond");
alert(msg);

When you run this code snippet, you will get two alert box, one from the first function and second with the message returned from the second function. The function sayHello is actually returning a value which can be stored in any variable or can be used directly.

Anonymous Function

The Anonymous function is the one which has no name, you can define it and it is executed without making any call to it. And Even better, you can define the variables in this function and prevent the global scope from being polluted.


(function(){
	alert("Hi! I am anonymous function");
})();

Callback Function

The simplest definition of a callback function is, "the function which can be passed into the another function as parameter". Again there is no necessity of the returning of the value. In practical programming, you will come across callback functions many times. Because JavaScript provides many inbuilt methods we are using one of them.


window.addEventListener("load",function(){ alert("this will alert on body load"); },false);

This method is a inbuilt method of JavaScript which is binding a load event with the anonymous function, which is the callback function we are talking about. The method will get called when the document loads.

Method

Method is not very separate from functions, but only when we put a function inside an object it is known as method. There are many inbuilt methods in JavaScript, including the alert() and as we saw in the previous example, addEventListener(). Here we are going to create a sample method.


var obj = {
	"method1": function(){
		alert("this is method 1");
	},
	"method2": function(){
		alert("this is method 2");
	}
};

obj.method1; //method call

Here we leave you with the functions of JavaScript, please provide your responses and correct us if we have made any mistake.