JavaScript Function Parameters

Function parameters in JavaScript act as placeholders for values that the function can accept when it’s called.

Syntax: 

function Name(paramet1, paramet2, paramet3,...) {
    // Statements
}

Rules: 

  • There is no need to specify the data type for parameters in JavaScript function definitions.
  • It does not perform type-checking based on the passed-in JavaScript functions.
  • It does not check the number of received arguments.

Parameters: 

  • Name: It is used to specify the name of the function.
  • Arguments: It is provided in the argument field of the function.

These are the types of parameters that can be used in JavaScript

  • Defaults Parameter
  • Function Rest Parameter
  • Arguments Object
  • Arguments Pass by Value
  • Objects passed by Reference

JavaScript Function Parameters Examples

1. Defaults Parameter

Default parameters in JavaScript are utilised to set initial values for named parameters in case no value or undefined is passed when the function is called.

Syntax: 

function Name(paramet1 = value1, paramet2 = value2 .. .) {
    // statements
}

Example: This example uses default parameters and performs the multiplication of numbers. 

Javascript
function GFG(num1, num2 = 2) {
    return num1 * num2;
}

console.log(GFG(4));

Output
8

2. Function Rest Parameter

In JavaScript, the rest parameter syntax enables a function to accept an unlimited number of arguments, which are then gathered into an array.

Example: here’s an example of using the rest parameter syntax in a function

Javascript
function sum(...numbers) {
  return numbers.reduce((acc, num) => acc + num, 0);
}

console.log(sum(1, 2, 3)); // Output: 6
console.log(sum(1, 2, 3, 4, 5)); // Output: 15
console.log(sum(10)); // Output: 10
console.log(sum()); // Output: 0

Output
6
15
10
0

Explanation: The sum function accepts any number of arguments and calculates their sum using the rest parameter ...numbers

3. Arguments Object

The arguments object is an inherent feature in JavaScript functions. It serves as a local variable in all non-arrow functions. You can analyze the arguments passed to a function using its arguments object.

Example: This example uses argument objects as parameters and finds the largest of numbers. 

Javascript
function GFG() {
    let i;
    let maxnum = -Infinity;
    for (i = 0; i < arguments.length; i++) {
        if (arguments[i] > maxnum) {
            maxnum = arguments[i];
        }
    }
    return maxnum;
}
console.log(GFG(10, 12, 500, 5, 440, 45));

Output
500

4. Arguments Pass by Value

In a function call, the parameters are called as arguments. The pass-by value sends the value of the variable to the function. It does not send the address of the variable. If the function changes the value of arguments then it does not affect the original value. 

Example: This example demonstrates the above-used approach.

Javascript
/* Function definition */
function w3wiki(var1, var2) {
    console.log("Inside the w3wiki function");

    var1 = 100;
    var2 = 200;

    /* Display the value of variable inside function */
    console.log("var1 =" + var1 + " var2 =" + var2);

}

var1 = 10;
var2 = 20;

/* The value of variable before Function call */
console.log("Before function calling");

console.log("var1 =" + var1 + " var2 =" + var2);

/* Function call */
w3wiki(var1, var2);

/* The value of variable after Function call */
console.log("After function calling");

console.log("var1 =" + var1 + " var2 =" + var2);

Output
Before function calling
var1 =10 var2 =20
Inside the w3wiki function
var1 =100 var2 =200
After function calling
var1 =10 var2 =20

Explanation:

  • Initially, var1 is assigned the value 10 and var2 is assigned the value 20.
  • When the w3wiki function is called with var1 and var2 as arguments, it modifies the values of var1 and var2 to 100 and 200 respectively within its scope.
  • However, outside the function, the values of var1 and var2 remain unchanged, demonstrating that JavaScript passes arguments by value, not by reference.

5. Objects passed by Reference

In Pass by Reference for objects, the function receives the address of the variable rather than the value itself as the argument. If we alter the value of the variable inside the function, it affects the variables outside the function as well.

Example: This example demonstrates the above-used approach.

Javascript
function w3wiki(varObj) {
    console.log("Inside w3wiki function");

    varObj.a = 100;
    console.log(varObj.a);

}

// Create object
varObj = { a: 1 };

/* Display value of object before function call */
console.log("Before function calling");
console.log(varObj.a);

/* Function calling */
w3wiki(varObj)

/* Display value of object after function call */
console.log("After function calling");
console.log(varObj.a);

Output
Before function calling
1
Inside w3wiki function
100
After function calling
100

Explanation:

  • Initially, an object varObj with property a set to 1 is created.
  • When the w3wiki function is called with varObj as an argument, it modifies the property a of varObj to 100 within its scope.
  • After the function call, the property a of varObj remains modified outside the function, demonstrating that objects are passed by reference in JavaScript.