Function Parameters and Arguments in JavaScript

Parameters in JavaScript are inputs that we provide in a function, on which the function operates on or perform calculations on, and gives us the output.

Note: We can have more than one parameters in a single function. The following example uses only one parameter so as to make it easy for you to understand.

Make sure you check out our previous JavaScript Functions lesson to understand how functions are declared and called.

Example

// Here, "name" is a parameter that we provided in our function called "helloWorld"
function helloWorld(name) {
    console.log("Hello World! I am " + name);
}

// We call the function and provide the string 'Ram' as Arguments.
// What are arguments? You will learn in a second below.
helloWorld('Ram');

Output

Hello World! I am Ram

Difference between Parameters and Arguments 🤔

Parameters are inputs provided to a function during function declaration, whereas arguments are real values provided to a function when a function is called or executed.

Parameters are used to explain how many inputs can a function take whereas arguments are the real values of those inputs provided when a function is called.

Example

// Here, "length" and "breadth" are parameters of the function called "area".
function area(length, breadth) {
    console.log(length * breadth);
}

// Here, the real values "5" and "10" are arguments.
area(5, 10);

Output

50

Default Parameters in JavaScript 🤯

Default parameters are those parameters whose initial values are already given by default. If we don’t specify the arguments when calling the function, the default or initial values will get provided into the function.

Example

// We provide 5 and 10 as default values to num1 and num2.
function sum(num1 = 5, num2 = 10) {
    console.log(num1 + num2);
}

sum(); // Output is 15. As num1 is 5 and num2 is 10 due to default parameters.
sum(2, 3); // Output is 5. As we provided arguments 2 and 3, the initial or default parameters 5 and 10 are replaced when calling the function.

// Also, we can only provide the first one and not provide the second one
sum(2); // Here, num1 is 2 and num2 is 10 (by default)

Output

15
5
12

In this way, we can provide input values (as many as possible) to a function in JavaScript using parameters and arguments.