1// For arrow functions, rest parameters should be preferred.
2let functionExpression = (...args) => {
3 console.log("Arguments", args)
4};
1function my_log(...args) {
2 // args is an Array
3 console.log(args);
4 // You can pass this array as parameters to another function
5 console.log(...args);
6}
7
1function add() {
2 var sum = 0;
3 for (var i = 0, j = arguments.length; i < j; i++) {
4 sum += arguments[i];
5 }
6 return sum;
7}
8
9add(2, 3, 4, 5); // 14
10
1function sum(...values) {
2 console.log(values);
3}
4sum(1);
5sum(1, 2);
6sum(1, 2, 3);
7sum(1, 2, 3, 4);
8
9
10function sum(...values) {
11 let sum = 0;
12 for (let i = 0; i < values.length; i++) {
13 sum += values[i];
14 }
15
16 return sum;
17}
18console.log(sum(1)); //1
19console.log(sum(1, 2)); //3
20console.log(sum(1, 2, 3)); // 5
21console.log(sum(1, 2, 3, 4)); //10
1function example() {
2 console.log(arguments);
3 console.log(arguments[0]);
4} // Console outputs an array of each argument with its value
5
6example('hi', 'hello');
7// Outputs:
8// ['hi', 'hello']
9// 'hi'