Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

JavaScript function definition


May 06, 2021 JavaScript


Table of contents


The JavaScript function defines


JavaScript defines functions using the keyword function.

Functions can be defined by declaration or by an expression.


Function declaration

In the previous tutorial, you learned about the syntax of function declarations:

function functionName ( parameters ) {
The code that was executed
}

Function declarations are not executed immediately and are called when we need them.

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

Try it out . . .

JavaScript function definition A sign is used to separate executable JavaScript statements.
Because the function declaration is not an executable statement, it does not end with a sign.

The function expression

JavaScript functions can be defined by an expression.

Function expressions can be stored in variables:

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

Try it out . . .

After a function expression is stored in a variable, the variable can also be used as a function:

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

Try it out . . .

The above function is actually an anonymous function (the function has no name).

Functions are stored in variables and do not require function names, which are usually called by variable names.

JavaScript function definition The above function ends with a sign because it is an execution statement.


Function() constructor

In the above example, we learned that functions are defined by the keyword function.

Functions can also be defined by the built-in JavaScript function constructor (Function)).

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

var x = myFunction(4, 3);

Try it out . . .

In fact, you don't have to use constructors. The above example can be written as:

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

var x = myFunction(4, 3);

Try it out . . .
JavaScript function definition In JavaScript, there are times when you need to avoid using the new keyword.


Function Promotion (Hoisting)

We've seen "hoisting" in previous tutorials.

Hoisting is JavaScript's default behavior to elevate the current scope to the front.

Hoisting is applied to the declaration of variables and the declaration of functions.

Therefore, the function can be called before declaring:

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

You cannot promote a function when you define it with an expression.


Self-calling function

Function expressions can be "self-called."

The self-calling expression is called automatically.

If the expression is followed by (), it is automatically called.

Y cannot self-call declared functions.

By adding parentheses, it is a function expression:

(function () {
var x = "Hello!!"; I 'll call myself
})();

Try it out . . .

The above function is actually an anonymous self-called function (no function name).


The function can be used as a value

The JavaScript function is used as a value:

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

var x = myFunction(4, 3);

Try it out . . .

JavaScript functions can be used as expressions:

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

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

Try it out . . .


The function is an object

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

However, the JavaScript function is more accurate in describing an object.

JavaScript functions have properties and methods.

The arguments.length property returns the number of arguments received by the function call procedure:

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

Try it out . . .

The toString() method returns the function as a string:

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

var txt = myFunction.toString();

Try it out . . .
JavaScript function definition A function defines a property as an object, called an object method.
A function is called an object constructor if it is used to create a new object.

JavaScript function exercises

JavaScript function definition

In JavaScript, extract the duplicate parts of the code and put them in a function.

JavaScript defines functions with parameters

The parameters of the JavaScript function, parameters, act as placeholders (also known as parameters), and the arguments can be one or more. T he arguments passed in when a function is called are arguments, which determine the true value of the parameters.