1function addition(...nombre) {
2
3 let resultat = 0
4
5 nombre.forEach(nombre => {
6 resultat+= nombre ;
7 });
8
9 console.log(resultat) ;
10
11}
12
13addition(4 ,9 ,5 ,415 ,78 , 54) ;
1// Before rest parameters, "arguments" could be converted to a normal array using:
2
3function f(a, b) {
4
5 let normalArray = Array.prototype.slice.call(arguments)
6 // -- or --
7 let normalArray = [].slice.call(arguments)
8 // -- or --
9 let normalArray = Array.from(arguments)
10
11 let first = normalArray.shift() // OK, gives the first argument
12 let first = arguments.shift() // ERROR (arguments is not a normal array)
13}
14
15// Now, you can easily gain access to a normal array using a rest parameter
16
17function f(...args) {
18 let normalArray = args
19 let first = normalArray.shift() // OK, gives the first argument
20}
21
1function multiply(multiplier, ...theArgs) {
2 return theArgs.map(element => {
3 return multiplier * element
4 })
5}
6
7let arr = multiply(2, 1, 2, 3)
8console.log(arr) // [2, 4, 6]
9
1function fun1(...theArgs) {
2 console.log(theArgs.length);
3}
4
5fun1(); // 0
6fun1(5); // 1
7fun1(5, 6, 7); // 3
8