JavaScript functions may be embedded

Any programming language is extended by defining functions. They are defined with the function keyword, followed by the name, a list of arguments between parenthesis and then the body of the function enclosed between curly braces:

function (a, b) 
  var x = a + b; 
  return x; 

Note that the arguments are not preceded by the var keyword, that is useless, unlike variables declared in the body of the function.
Parameters are passed by values for all variables but not not when they are objects.
Thus if one changes the contents of an object in the body of a function, these modifications remain after the call of the function.

The function can return nothing, or return one value. In the second case the return keyword is used.

Once defined, a function is called according to the following format:

function mult(y) { ... }

mult(23);            // call without return value
z + 3 + mult(44)     // inside an expression a value must be returned
mult(b + 6)          // variable or expression as parameter

Optional parameters with default values

The arguments of a JavaScript function are all optional because the interpreter places the parameters in an array, and finds them internally by the name of variable which is used as index.


function myFun (x, y, z)

The function may be called by:

myFun (10);     // or 
myFun (has, 5); // or 
myFun (1, 2, 3);

In fact x, y, z are for the interpreter the keys of an associative table whose values are the parameters passed at the call of the function.

At the opposite the programmer can add at call time parameters not planned in its declaration. For example:

myFun (0, 10, 20, 30, 40, 50, 60);

The added parameters are taken into account in the array of arguments, which is a keyword of the language and is called arguments. The programmer finds the parameters by their position in the array, the first having the 0 index.

The predefined attributes of argument are:
length: the number of elements in the array.
callee: the name of the function of which they are the arguments.

In the previous example of call of function with supernumerary parameters, this line placed in the body of the function:

alert (arguments [3]);

would post 30.

Example showing parameters may be added when a function is called

function myFun(x, y) 
var result=x;
result += "-";
result += y;
for (var i = 2; i < arguments.length; i++)
result += "-" + arguments[i];
return result;
function test()
var x = myFun("a", "b", "c", "d", "e") ;

JavaScript does not recognize default values​​. For example function x(a = 33) is invalid in this language while it is supported in PHP or C.

We can use various alternatives such as to test in the function body if the variable is declared to otherwise assign a value. Example:

var myFun = function(x) 
if (x === undefined) { x = 33; }

Example of a function declared inside another and how to access it

It is possible to define functions in the declaration of another function, and usual scoping rules are applied.
The variables of the outer function are accessible to the inner function.

function outer(a)
   function inner(b)
      return b * 2 + a;

   return inner(a)

var z = outer(5);


You can actually use functions to create objects and add methods as internal functions.

See also

© 2007-2012