1// With Rest Parameter
2let sum = (...nums) => {
3 let total = 0;
4 for (let num of nums) {
5 total += num;
6 }
7 return total;
8};
9console.log(sum(4, 23, 65, 2)); // 94
10
11
12// Before
13function sum() {
14 let total = 0;
15 for (let argument of arguments) {
16 total += argument;
17 }
18 return total;
19}
20console.log(sum(4, 23, 65, 2)); // 94
1var sum = 0;
2function addAll(){
3 for (var i = 0; i<arguments.length; i++){
4 sum+=arguments[i];
5 }
6 console.log(sum);
7}
8
9addAll(1, 2, 3, 4, 5, 6, 7, 8, 9,10); //we can provide inifite numbers as argument
1function test(a, b, c){
2 // console.log(arguments);
3 // console.log(JSON.stringify(arguments));
4 // console.log(typeof a);
5
6 // var sum = 0;
7 // for (var i = 0; i<arguments.length; i++){
8 // sum += arguments[i];
9 // }
10 // console.log(sum);
11
12 //for (var i = 0; i<arguments.length; i++){
13 // console.log(arguments[i]);
14 //}
15
16 console.log(arguments[0]);
17}
18
19test(10, 20, 30);