mozilla
Your Search Results

    Function

    A function is a code snippet that can be called by other code or by itself, or a variable that refers to the function. When a function is called, arguments are passed to the function as input, and the function can optionally return an output. A function in JavaScript is also an object.

    A function name is an identifier declared as part of a function declaration or function expression. The function name's scope depends on whether the function name is a declaration or expression.

    Different types of functions

    An anonymous function is a function without a function name:

    function () {}

    A named function is a function with a function name:

    function foo() {}

    An inner function is a function inside another function (square in this case). An outer function is a function containing a function (addSquares in this case):

    function addSquares(a,b) {
       function square(x) {
          return x * x;
       }
       return square(a) + square(b);
    }

    A recursive function is a function that calls itself. See recursion.

    function loop(x) {
       if (x >= 10)
          return;
       loop(x + 1);
    }

    Learn more

    Technical reference

    Document Tags and Contributors

    Contributors to this page: fscholz, klez, Andrew_Pfeiffer, hbloomer
    Last updated by: Andrew_Pfeiffer,