Parameters & Arguments in JavaScript

link

  • Parameters are variables listed as a part of the function definition.
  • Arguments are values passed to the function when it is invoked.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Basic function with three parameters that logs the sum of all the parameters
function argCheck(parameter1, parameter2, parameter3){
console.log(parameter1 + parameter2 + parameter3);
}

// Function with extra arguments
argCheck(1,2,3,4);
// Logs 6 (1 + 2 + 3, ignores 4)

// Function with missing arguments
argCheck(1,2);
// Logs NaN because by default if a corresponding argument is missing, it is set to undefined.
// parameter3 is assigned undefined and so 1+2+undefined = NaN

// Note that, no error is thrown
1
2
3
4
5
6
7
8
9
10
11
12
function argumentVar(parameter1, parameter2, parameter3){
console.log(arguments.length); // Logs the number of arguments passed.
console.log(arguments[3]); // Logs the 4th argument. Follows array indexing notations.
}

argumentVar(1,2,3,4,5);
// Log would be as follows
// 5
// 4

// 5 is the number of arguments
// 4 is the 4th argument
1
2
3
4
5
6
7
8
9
10
11
12
function restParam(parameter1, ...restArgs){
console.log(restArgs.length); // Logs the number of arguments that do not have a corresponding parameter
console.log(restArgs[2]); // Logs the 3rd argument after the number of arguments that do not have a corresponding parameter
}

restParam(1,2,3,4,5);
// Log would be as follows
// 4
// 4

// 4 is the number of arguments that do not have a corresponding parameter
// 4 is the 4th 3rd argument after the number of arguments that do not have a corresponding parameter