The usage of the rest parameter of javascript

rest parameter of javascript, as follows:

clipboard.png

question:
there is a sentence in the above picture, "the variable of the rest parameter is an array", but add (2Power5) , the parameter passed is not an array, how should it be understood?

Dec.06,2021

means .valuse equals [2Magazine 3]
actually you should see deconstruct , otherwise such things can be explained all day.

function test({a, b, ...rest}){
    console.log(rest);//{ c: 30, d: 40 }
}

test({a: 10, b: 20, c: 30, d: 40});
function f(...[a, b, c]) {
  return a + b + c;
}

f(1, 2, 3)   

function f1(a, b, c) {
    return a + b + c;
}

function f2(...param) {
  return a + b + c;
}

let arr = [1, 2, 3];

f2 (arr); is equivalent to F1 (1,2,3);

if you pass in several parameters instead of an array, it treats the arguments as an array

what is more specific is that you have learned it by yourself. You can't say it clearly in three or two sentences


reset
function add(...values) {
  console.log(values, typeof values)
  let sum = 0;

  for (var val of values) {
    sum += val;
  }

  return sum;
}

this means that multiple parameters passed will be automatically assembled into an array


by looking at the example, you can see that array refers to values ah.

Menu