+ 1

Rest Parameters in ES6

what does these following lines mean? function containsAll(arr) { for (let k = 1; k < arguments.length; k++) { let num = arguments[k]; if (arr.indexOf(num) === -1) { return false; } } return true; } let x = [2, 4, 6, 7]; console.log(containsAll(x, 2, 4, 7)); console.log(containsAll(x, 6, 4, 9)); please can anyone explain me this code?

2nd Apr 2020, 10:29 AM
Muhammad Awwab Khan
Muhammad Awwab Khan - avatar
1 Réponse
+ 4
`arguments` is like an array and it contains all arguments passed to your function. For example if you have something(x, 2, 4, 7); then arguments[0] == x arguments[1] == 2 arguments[2] == 4 arguments[3] == 7 However, this is not nice ES6 code. In ES6 we should to do this instead: function containsAll(arr, ...rest) { for(let num of rest) { if(arr.indexOf(num) === -1) return false; } return true; }
2nd Apr 2020, 10:36 AM
Schindlabua
Schindlabua - avatar