javascript args 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: js unspecified parameters
function my_log(...args) {
console.log(args);
console.log(...args);
}
Example 3: node js function infinite parameters
function print(...args) {
args.forEach((arg, index) => {
console.log(arg);
});
}
print('hello', true, new Date());
Example 4: arguments object in javascript
var sum = 0;
function addAll(){
for (var i = 0; i<arguments.length; i++){
sum+=arguments[i];
}
console.log(sum);
}
addAll(1, 2, 3, 4, 5, 6, 7, 8, 9,10);
Example 5: how to access any argument in javascript
function example() {
console.log(arguments);
console.log(arguments[0]);
}
example('hi', 'hello');
Example 6: arguments object in javascript
function test(a, b, c){
console.log(arguments[0]);
}
test(10, 20, 30);