javascript function as argument code example

Example 1: javascript function variable arguments

function foo() {
  for (var i = 0; i < arguments.length; i++) {
    console.log(arguments[i]);
  }
}

foo(1,2,3);
//1
//2
//3

Example 2: javascript pass function as parameter

//passing a function as param and calling that function
function goToWork(myCallBackFunction) {
    //do some work here
    myCallBackFunction();
}

function refreshPage() {
    alert("I should be refreshing the page");
}

goToWork(refreshPage);

Example 3: how to access any argument in javascript

function example() {
	console.log(arguments);
  	console.log(arguments[0]);
} // Console outputs an array of each argument with its value

example('hi', 'hello'); 
// Outputs: 
// ['hi', 'hello']
// 'hi'