SoFunction
Updated on 2025-03-03

JavaScript method to define functions

JavaScript uses the keyword function to define functions.

Functions can be defined by declarations or can be an expression.

Function declaration
In the previous tutorial, you have already understood the syntax of function declarations:

function functionName(parameters) {
Executed code
}

The function will not be executed immediately after being declared and will be called when we need it.
Example

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

Function expressions

JavaScript functions can be defined by an expression.
Function expressions can be stored in variables:

Example

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

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

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

The above function is actually an anonymous function (the function has no name).
Functions are stored in variables and do not require a function name. They are usually called by variable names.

Function() constructor
In the above example, we understand that functions are defined by the keyword function.
Functions can also be defined through the built-in JavaScript function constructor (Function()).

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

Actually, you don't have to use a constructor. The above example can be written as:

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

Function promotion (Hoisting)
In previous tutorials, we have learned about "hoisting".
Hoisting is the behavior of JavaScript to upgrade the current scope to the previous one by default.
Hoisting applies to variable declarations and function declarations.
Therefore, the function can be called before the declaration:

myFunction(5);

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

It is not possible to improve when defining functions using expressions.
Self-call function
Function expressions can be "self-called".
The self-call expression will be called automatically.
If the expression is followed by () , it will be called automatically.
Cannot call declared functions themselves.
By adding parentheses, it is an function expression:

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

Of course, you can also write as follows:

!function(){}();
+function(){}();
-function(){}();
~function(){}();
~(function(){})();
void function(){}();
(function(){}());

The most commonly used method is the first method.

The above function is actually an anonymous self-call function (without function name).
Functions can be used as a value
JavaScript functions are used as a value:

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

var x = myFunction(4, 3);

JavaScript functions can be used as expressions:

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

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

Functions are objects
Using the typeof operator in JavaScript to determine that the function type returns "function" .
But JavaScript functions are more accurate to describe as an object.
JavaScript functions have properties and methods.
The attribute returns the number of parameters received by the function call process:

function myFunction(a, b) {
  return ;
}

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

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

var txt = ();

Function definitions are attributes of objects, called object methods.
If a function is used to create a new object, it is called the object's constructor.

Here are example codes for various methods

<html> 
<head></head> 
<body> 
<script type="text/javascript"> 
/* Javascript can define functions (declare functions) in three ways: normal method, constructor, and direct function quantity.  */ 
/*1. Normal method function(param){}*/ 
function print(msg) 
{ 
(msg,"<br/>"); 
} 
/*If the function does not contain a return statement, only the statement inside the function body is executed and undefined*/ 
/*2.Constructor method: new Function()*/ 
var add1=new Function('a','b','return a+b'); 
/*3. Function direct quantity method, create unnamed functions, */ 
var result = function(x,y){return x+y;}; 
/*You can also specify the function name*/ 
var result2 = function fact(x){if(x<1) return 1;else return x*fact(x-1)}; 
('Call the general method:'); 
print("<hr/>"); 
print('Calling the constructor method:add1(5,6)'); 
print(add1(5,6)); 
print("<hr/>"); 
print("Calling the function direct quantity method:result(3,4)"); 
var re =result(3,4); 
print(re); 
print("Calling the function direct quantity method:result2(3)"); 
print(result2(3)); 
print("<hr/>"); 
print('Functions are used as data'); 
/* Functions can be used as data*/ 
function add(x,y){return x+y;} 
function subtract(x,y){return x-y;} 
function multiply(x,y){return x*y;} 
function divide(x,y){return x/y;} 
function operate(operator,operand1,operand2) 
{ 
return operator(operand1,operand2); 
} 
//Calculate (2+3) + (4*5)var i = operate(add,operate(add,2,3),operate(multiply,4,5)); 
print('(2+3) + (4*5)='+i); 
print("<hr/>"); 
//Use function to directly countvar operators = new Object(); 
operators['add'] = function(x,y){return x+y;} 
operators['substract'] = function(x,y){return x-y;} 
operators['multiply'] = function(x,y){return x*y;} 
operators['divide'] = function(x,y){return x/y;} 
operators['pow'] = ; 
function operate2(op_name,operand1,operand2) 
{ 
if(operators[op_name] == null) return "unknown operator"; 
else return operators[op_name](operand1,operand2); 
} 
//Define "hello" + "" + "world"var j = operate2("add","hello",operate2("add"," ","world")); 
var k = operate2("pow",10,2); 
print(j); 
print(k); 
print("<hr/>"); 
</script> 
</body> 
</html> 

The running result is:
Calling general methods:
--------------------------------------------------------------------------------
Call constructor method: add1(5,6)
11
--------------------------------------------------------------------------------
Calling the function direct quantity method: result(3,4)
7
Calling the function direct quantity method: result2(3)
6
--------------------------------------------------------------------------------
Functions are used as data
(2+3) + (4*5)=25
--------------------------------------------------------------------------------
hello world
100