EN
JavaScript - create and use functions
7 points
In this article, we would like to show you how to create and use functions in JavaScript.
xxxxxxxxxx
1
var action1 = Function('console.log("This is my message...");');
2
var action2 = Function('console.log(arguments);');
3
var action3 = new Function('console.log("This is my message...");');
4
var action4 = new Function('console.log(arguments);');
5
6
action1();
7
action2(1, 2, 3, 'test 1', 'text 2', 'text n');
8
action3();
9
action4(1, 2, 3, 'test 1', 'text 2', 'text n');
10
11
console.log(action1.name);
12
console.log(action2.name);
13
console.log(action3.name);
14
console.log(action4.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
3
[Arguments] {
4
'0': 1,
5
'1': 2,
6
'2': 3,
7
'3': 'test 1',
8
'4': 'text 2',
9
'5': 'text n'
10
}
11
12
This is my message...
13
14
[Arguments] {
15
'0': 1,
16
'1': 2,
17
'2': 3,
18
'3': 'test 1',
19
'4': 'text 2',
20
'5': 'text n'
21
}
22
23
anonymous
24
anonymous
25
anonymous
26
anonymous
xxxxxxxxxx
1
var action = function() {
2
console.log('This is my message...');
3
};
4
5
var sum1 = function(a, b) {
6
return a + b;
7
};
8
9
var sum2 = function() {
10
var result = 0;
11
12
for(var i = 0; i < arguments.length; ++i) {
13
result += arguments[i];
14
}
15
16
return result;
17
};
18
19
var print = function(a, b) {
20
var text = '';
21
22
text += 'a=' + a + ', ';
23
text += 'b=' + b + ', ';
24
text += 'arguments=[';
25
26
for(var i = 0; i < arguments.length; ++i) {
27
if(i > 0) {
28
text += ', ';
29
}
30
text += arguments[i];
31
}
32
33
text += ']';
34
35
console.log(text);
36
};
37
38
39
action();
40
console.log(sum1(1, 2));
41
console.log(sum2(1, 2, 3, 4, 5));
42
print(1, 2, 3, 4, 5);
43
44
console.log(action.name);
45
console.log(sum1.name);
46
console.log(sum2.name);
47
console.log(print.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
3
3
15
4
a=1, b=2, arguments=[1, 2, 3, 4, 5]
5
6
action
7
sum1
8
sum2
9
print
xxxxxxxxxx
1
var action = function MyFunctionName() {
2
console.log('This is my message...');
3
};
4
5
var sum1 = function MyFunctionName(a, b) {
6
return a + b;
7
};
8
9
var sum2 = function MyFunctionName(a, b) {
10
var result = 0;
11
12
for(var i = 0; i < arguments.length; ++i) {
13
result += arguments[i];
14
}
15
16
return result;
17
};
18
19
var print = function MyFunctionName(a, b) {
20
var text = '';
21
22
text += 'a=' + a + ', ';
23
text += 'b=' + b + ', ';
24
text += 'arguments=[';
25
26
for(var i = 0; i < arguments.length; ++i) {
27
if(i > 0) {
28
text += ', ';
29
}
30
text += arguments[i];
31
}
32
33
text += ']';
34
35
console.log(text);
36
};
37
38
39
action();
40
console.log(sum1(1, 2));
41
console.log(sum2(1, 2, 3, 4, 5));
42
print(1, 2, 3, 4, 5);
43
44
console.log(action.name);
45
console.log(sum1.name);
46
console.log(sum2.name);
47
console.log(print.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
3
3
15
4
a=1, b=2, arguments=[1, 2, 3, 4, 5]
5
6
MyFunctionName
7
MyFunctionName
8
MyFunctionName
9
MyFunctionName
Arrow function has been introduced in ES6. This part shows in a simple way how to use arrow functions.
3.1. With function body in brackets
xxxxxxxxxx
1
var action1 = () => {
2
console.log('This is my message...');
3
};
4
5
var action2 = (myArguments) => {
6
console.log(myArguments);
7
};
8
9
var sum = (a, b) => {
10
return a + b;
11
};
12
13
var print = (a, b, myArguments) => {
14
var text = '';
15
16
text += 'a=' + a + ', ';
17
text += 'b=' + b + ', ';
18
text += 'myArguments=[';
19
20
for(var i = 0; i < myArguments.length; ++i) {
21
if(i > 0) {
22
text += ', ';
23
}
24
text += myArguments[i];
25
}
26
27
text += ']';
28
29
console.log(text);
30
};
31
32
33
action1();
34
action2(1, 2, 3, 4, 5);
35
console.log(sum(1, 2));
36
print(1, 2, 3, 4, 5);
37
38
console.log(action1.name);
39
console.log(action2.name);
40
console.log(sum.name);
41
console.log(print.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
[ 1, 2, 3, 4, 5 ]
3
3
4
a=1, b=2, myArguments=[3, 4, 5]
5
6
action1
7
action2
8
sum
9
print
3.2. Without brackets - with auto-returning of values
xxxxxxxxxx
1
var action = () => console.log('This is my message...');
2
3
var print1 = text => console.log(text);
4
var print2 = (myArguments) => console.log(myArguments);
5
var print3 = (a, b, myArguments) => console.log(a, b, myArguments);
6
7
var sum = (a, b) => a + b;
8
9
10
action();
11
print1('This is my custom text...');
12
print2(1, 2, 3, 4, 5);
13
print3(1, 2, 3, 4, 5);
14
console.log(sum(1, 2));
15
16
console.log(action.name);
17
console.log(print1.name);
18
console.log(print2.name);
19
console.log(print3.name);
20
console.log(sum.name);
Output (with NodeJS):
xxxxxxxxxx
1
This is my message...
2
This is my custom text...
3
[ 1, 2, 3, 4, 5 ]
4
1 2 [ 3, 4, 5 ]
5
3
6
7
action
8
print1
9
print2
10
print3
11
sum
The ways how to call function (method) has been described in this article.