Latest web development tutorials
×

JavaScript Tutorial

JavaScript Tutorial JavaScript Introduction JavaScript usage JavaScript Output JavaScript grammar JavaScript Statement JavaScript Notes JavaScript variable JavaScript type of data JavaScript Object JavaScript function JavaScript Scope JavaScript event JavaScript The string JavaScript Operator JavaScript Comparison JavaScript If...Else JavaScript switch JavaScript for JavaScript while JavaScript Break & Continue JavaScript typeof JavaScript Type conversion JavaScript Regular Expressions JavaScript error JavaScript debugging JavaScript Variable promotion JavaScript Strict mode JavaScript Use errors JavaScript Form validation JavaScript Keep the keyword JavaScript JSON JavaScript void JavaScript Code specification

JS function

JavaScript Function definition JavaScript Function parameter JavaScript Function call JavaScript Closure

JS HTML DOM

DOM Introduction DOM HTML DOM CSS DOM event DOM EventListener DOM element

JS Advanced Tutorial

JavaScript Object JavaScript Number JavaScript String JavaScript Date JavaScript Array JavaScript Boolean JavaScript Math JavaScript RegExp Object

JS Browser BOM

JavaScript Window JavaScript Window Screen JavaScript Window Location JavaScript Window History JavaScript Navigator JavaScript Pop-ups JavaScript Timing events JavaScript Cookies

JS Library

JavaScript Library JavaScript test jQuery JavaScript test Prototype

JS Examples

JavaScript Examples JavaScript Object instance JavaScript Browser object instance JavaScript HTML DOM Examples JavaScript to sum up

JS Reference Manual

JavaScript Object HTML DOM Object

JavaScript function definition

JavaScript function is defined using the keyword function.

You can define a function declaration, it can be an expression.


Function declaration

In the previous tutorial, you already know the syntax of a function declaration:

function functionName (parameters) {
Code execution
}

After the function declaration does not happen immediately, it will call when we need to.

Examples

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

try it"

Note The semicolon is used to separate executable JavaScript statement.
Since the function declaration is not an executable statement, so it does not end with a semicolon.


Function expression

JavaScript functions can be defined by an expression.

Function expression may be stored in a variable:

Examples

var x = function (a, b) {return a * b};

try it"

After the function expression is stored in a variable, but also as a function Use:

Examples

var x = function (a, b) {return a * b};
var z = x (4, 3);

try it"

The above function is actually an anonymous function (function without a name).

Functions are stored in a variable, the function name is not required, usually called by the variable name.

Note Above functions with a semicolon, because it is an executable statement.


Function () constructor

In the above example, we learned that functions with the keyword function definitions.

The same function can (Function ()) defined by the built-in JavaScript function constructor.

Examples

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

var x = myFunction (4, 3);

try it"

In fact, you do not have to use the constructor. Examples of the above can be written as:

Examples

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

var x = myFunction (4, 3);

try it"

Note In JavaScript, a lot of the time, you need to avoid using the new keyword.


Function to enhance (Hoisting)

In the previous tutorial, we have learned "hoisting (lifting)."

Lift (Hoisting) JavaScript is the default of the current scope to the front to enhance the behavior.

Lift (Hoisting) Application Disclaimer and function of variables.

Therefore, the function can be invoked before the statement:

myFunction (5);

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

Unable to enhance the use of expressions defined functions.


Since the function is called

Function expression can be "self-call."

Since the call expression invoked automatically.

If after an expression followed () is automatically called.

Function declaration can not call itself.

By adding brackets, to illustrate that it is a function expression:

Examples

(Function () {
var x = "Hello !!"; // I will call myself
}) ();

try it"

The above function is actually an anonymous function that calls itself (not the function name).


Function can be used as a value for use

JavaScript function as a value to use:

Examples

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

var x = myFunction (4, 3);

try it"

JavaScript functions can be used as expressions:

Examples

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

var x = myFunction (4, 3) * 2;

try it"


Functions are objects

Use typeof operator to determine the type of function in JavaScript returns "function".

But JavaScript function described as a target more accurately.

JavaScript functions have attributes and methods.

arguments.length property returns a function call received to the number of parameters:

Examples

function myFunction (a, b) {
return arguments.length;
}

try it"

toString () method will function as a string:

Examples

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

var txt = myFunction.toString ();

try it"

Note Function is defined as a property of the object, called object method.
If the function is used to create a new object, called object's constructor.