WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Function Definitions


JavaScript capacities are defined with the function keyword.

You can utilize a capacity declaration or a work expression.


Function Declarations

Earlier in this instructional exercise, you discovered that capacities are declared with the following syntax:

work functionName(parameters) {
  code to be executed
}

Declared capacities are not executed right away. They are "saved for later use", furthermore, will be executed later, when they are summoned (called upon).

Example

work myFunction(a, b) {
    return a * b;
}
Try it yourself »


Function Expressions

A JavaScript capacity can likewise be characterized utilizing a expression.

A work articulation can be put away in a variable:

Example

var x = work (a, b) {return a * b};
Try it Yourself »

After a capacity articulation has been put away in a variable, the variable can be utilized as a function:

Example

var x = work (a, b) {return a * b};
var z = x(4, 3);
Try it Yourself »

The work above is really a anonymous function (a capacity without a name).

Functions put away in factors needn't bother with capacity names. They are dependably conjured (called) utilizing the variable name.


The Function() Constructor

As you have found in the past precedents, JavaScript capacities are characterized with the function keyword.

Functions can likewise be characterized with an inherent JavaScript work constructor called Capacity().

Example

var myFunction = new Function("a", "b", "return a * b");

var x = myFunction(4, 3);
Try it Yourself »

You really don't need to utilize the capacity constructor. The precedent above is equivalent to writing:

Example

var myFunction = work (a, b) {return a * b};

var x = myFunction(4, 3);
Try it Yourself »


Function Hoisting

Earlier in this instructional exercise, you found out about "hoisting".

Hoisting is JavaScript's default conduct of moving declarations to the highest point of the current scope.

Hoisting applies to variable revelations and to work declarations.

Because of this, JavaScript capacities can be called before they are declared:

myFunction(5);

work myFunction(y) {
    return y * y;
}

Functions characterized utilizing an articulation are not hoisted.


Self-Invoking Functions

Function articulations can be made "self-invoking".

A self-conjuring articulation is summoned (begun) consequently, without being called.

Function articulations will execute naturally if the articulation is pursued by ().

You can't self-summon a capacity declaration.

You need to include enclosures around the capacity to show that it is a capacity expression:

Example

(work () {
    var x = "Hello!!";     /I will summon myself
})();
Try it Yourself »

The work above is really a anonymous self-summoning function (work without name).


Functions Can Be Used as Values

JavaScript capacities can be utilized as values:

Example

work myFunction(a, b) {
    return a * b;
}

var x = myFunction(4, 3);
Try it Yourself »

JavaScript capacities can be utilized in expressions:

Example

work myFunction(a, b) {
    return a * b;
}

var x = myFunction(4, 3) * 2;
Try it Yourself »

Functions are Objects

The typeof administrator in JavaScript returns "function" for functions.

But, JavaScript capacities can best be portrayed as objects.

JavaScript capacities have both properties and methods.

The arguments.length property restores the quantity of contentions got when the capacity was invoked:

Example

work myFunction(a, b) {
    return arguments.length;
}
Try it Yourself »

The toString() technique restores the capacity as a string:

Example

work myFunction(a, b) {
    return a * b;
}

var txt = myFunction.toString();
Try it Yourself »