function foo() {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
foo(1,2,3);
//1
//2
//3
// For arrow functions, rest parameters should be preferred.
let functionExpression = (...args) => {
console.log("Arguments", args)
};
// what are the parameters and arguments in javascript
// Function parameters are the names listed in the function's definition.
// Function arguments are the real values passed to the function.
function calculateArea(width, height){ // width and height are Parameters
console.log*=(width * height);
}
calculateArea(2,3); // 2 and 3 are Arguments
function test()
{
console.log(arguments);
}
test(1, 2, 3, 4, 5, 6)
//[Arguments] { '0': 1, '1': 2, '2': 3, '3': 4, '4': 5, '5': 6 }
function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i < j; i++) {
sum += arguments[i];
}
return sum;
}
add(2, 3, 4, 5); // 14
function hello(name) {
alert("Hello " + name);
}
var name = "Person";
hello();
An Example of a function argument
function printValue(someValue) {
console.log('The item I was given is: ' + someValue);
}
printValue('abc'); // -> The item I was given is: abc