xxxxxxxxxx
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
xxxxxxxxxx
function foo() {
for (var i = 0; i < arguments.length; i++) {
console.log(arguments[i]);
}
}
foo(1,2,3);
//1
//2
//3
xxxxxxxxxx
// 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
xxxxxxxxxx
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 }
xxxxxxxxxx
// For arrow functions, rest parameters should be preferred.
let functionExpression = (args) => {
console.log("Arguments", args)
};
xxxxxxxxxx
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
xxxxxxxxxx
// program to print the text
// declaring a function
function greet(name) {
console.log("Hello " + name + ":)");
}
// variable name can be different
let name = prompt("Enter a name: ");
// calling function
greet(name);
xxxxxxxxxx
function hello(name) {
alert("Hello " + name);
}
var name = "Person";
hello();
xxxxxxxxxx
Function parameters are the names listed in the function definition.
Function arguments are the real values passed to (and received by) the function.
Parameter Rules
JavaScript function definitions do not specify data types for parameters.
JavaScript functions do not perform type checking on the passed arguments.
JavaScript functions do not check the number of arguments received.
Default Parameters
If a function is called with missing arguments (less than declared), the missing values are set to undefined.
Sometimes this is acceptable, but sometimes it is better to assign a default value to the parameter