The best JavaScript function definition Tutorial In 2024, In this tutorial you can learn Function declaration,Examples,Function expression,Examples,Examples,Function () constructor,Examples,Examples,Function to enhance (Hoisting),Since the function is called,Examples,Function can be used as a value for use,Examples,Examples,Functions are objects,Examples,Examples,

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;
}


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};

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);

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);

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);


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
}) ();

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);

JavaScript functions can be used as expressions:

Examples

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

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



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;
}

toString () method will function as a string:

Examples

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

var txt = myFunction.toString ();


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.
JavaScript function definition
10/30