-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path14-ES6ParameterHandling.js
37 lines (32 loc) · 994 Bytes
/
14-ES6ParameterHandling.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//******* EcmaScript 6: Default Parameter Values
function sum (x = 1, y = 2, z = 3) {
return x + y + z
};
console.log(sum(4,2)); //outpus 9
//function above is the same as
function sum2 (x, y, z) {
if (x === undefined)
x = 1;
if (y === undefined)
y = 2;
if (z === undefined)
z = 3;
return x + y + z;
};
console.log(sum2(4,2)); //outpus 9
//******* EcmaScript 6: spread operator ('...')
var params = [3, 4, 5];
console.log(sum(...params));
var numbers = [1, 2, ...params]; //pushing values into array
console.log(numbers);
//******* EcmaScript 6: rest parameter ('...')
function restParamaterFunction (x, y, ...a) {
return (x + y) * a.length;
}
console.log(restParamaterFunction(1, 2, "hello", true, 7)); // outputs 9;
//code above is the same as ES5:
function restParamaterFunction2 (x, y) {
var a = Array.prototype.slice.call(arguments, 2);
return (x + y) * a.length;
};
console.log(restParamaterFunction2(1, 2, "hello", true, 7));