wiki/content/20200702204226-optional_arguments.md

862 B

date id title
2020-07-02 6e1019ae-983e-42c6-90cf-daa6aebb86ea Optional arguments in JavaScript functions

JavaScript is extremely broad-minded about the number of arguments you pass to a function. If you pass too many, the extra ones are ignored. If you pass too few, the missing parameters get assigned the value undefined.

function square(x) { return x * x; }
console.log(square(4, true, "hedgehog"));
function minus(a, b) {
  if (b === undefined) return -a;
  else return a - b;
}

console.log(minus(10));
console.log(minus(10, 5));

Function parameters can also be given default values

function power(base, exponent = 2) {
  let result = 1;
  for (let count = 0; count < exponent; count++) {
    result *= base;
  }
  return result;
}

console.log(power(4));
console.log(power(2, 6));