defint a js function with int parameter code example
Example 1: javascript function multiple parameters
function sum(...values) {
console.log(values);
}
sum(1);
sum(1, 2);
sum(1, 2, 3);
sum(1, 2, 3, 4);
function sum(...values) {
let sum = 0;
for (let i = 0; i < values.length; i++) {
sum += values[i];
}
return sum;
}
console.log(sum(1)); //1
console.log(sum(1, 2)); //3
console.log(sum(1, 2, 3)); // 5
console.log(sum(1, 2, 3, 4)); //10
Example 2: parameters in javascript
function myFunction(x, y) {
if (y === undefined) {
y = 2;
}
}