mirror of
https://github.com/alrayyes/wiki.git
synced 2024-12-01 15:26:23 +00:00
845 B
845 B
id | title |
---|---|
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));