js function argument code example
Example 1: javascript function variable arguments
function foo() {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
foo(1,2,3);
Example 2: parameters types in javascript
function myFunction(a, b, amount){
}
Example 3: 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));
console.log(sum(1, 2));
console.log(sum(1, 2, 3));
console.log(sum(1, 2, 3, 4));
Example 4: how to access any argument in javascript
function example() {
console.log(arguments);
console.log(arguments[0]);
}
example('hi', 'hello');