A function contains a section of a program, i.e. a list of statements, which can be addressed by a specified name, or "called," independent of the rest of the program. A function's statements, known as its "body," will only be executed when it is called. To call a function, simply append parentheses () to its name.

Each function may define parameters - variables which, when the function is called, hold the corresponding values "passed" (assigned) in the function call. Those corresponding values are known as arguments, and they are only stored in the parameters within the function's body.

It may also return a value which a simple function call will be evaluated as. If no value is returned, the call evaluates to undefined.

Contents

When a function is assigned and called as a property of another object, it is known as a "method." Within a method, the keyword this refers to the function's "owner" - the object to which it is assigned as a property.

A function call prefixed by new generates or, more accurately, "constructs" a new object. Functions used for this purpose are called "constructors," and their names are typically capitalized to aid humans in identifying them correctly. The act of calling a constructor with new is known as "instantiating" the constructor, and the resulting object is an "instance" of the constructor. Inside the constructor's body, this is the current instance of the constructor.

Any variables declared with var inside a function will not be available from outside the same function. Each similar variable from outside the function will be available unless it shares its identifier with a local variable.
Let us assume that, for some reason, we want the current count to be normally inaccessible to the calling program. We can move both it and the function to the inside of the constructor along with a new "getter" - a function which simply returns a private value. As Douglas Crockford refers to such variables[1], the count will be "private," the functions as above were "public," and the functions will now be "privileged" (public but informed of private variables).

Variable arity is the fancy term used to characterize functions which can fill their formal parameters and take any number of additional arguments. JavaScript also allows a function's formal parameters to be ignored in calls to that function; in such a case, the empty parameters are set to undefined.

Dynamic binding occurs when a function assigned as a method on one object is also assigned to another object. In this case, which object does this refer to? According to JavaScript, whichever served as the function's namespace for any given call is this.