2

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);
3
  • 3
    It's called the spread operator. See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 13, 2019 at 3:18
  • 2
    Although it's worth mentioning that in this particular case it's a poor use, since the function itself won't actually handle more than two arguments. Commented Jan 13, 2019 at 3:21
  • It called spread operator. But in this sample code I think we don't need to do this. since your compare function only need two parameters (Assume this is real code). Commented Jan 13, 2019 at 4:25

1 Answer 1

1

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.

https://davidwalsh.name/spread-operator

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.