I just started learn Javascript, I'm confused about the function parameter ...[1,2]
, why the function parameter like this.
function compare(a, b) {
return a - b;
}
let result = compare(...[1,2]);
console.log(result);
I just started learn Javascript, I'm confused about the function parameter ...[1,2]
, why the function parameter like this.
function compare(a, b) {
return a - b;
}
let result = compare(...[1,2]);
console.log(result);
It's a new Es6 functionality called the spread operator, it's great for calling functions (without apply), converting arguments or NodeList to Arrays, array manipulations, and even when using the Math functions.