Table of contents
1.
Introduction
2.
What are Function Parameters in JavaScript?
3.
Syntax of Javascript Function Parameters
4.
Rules
5.
Parameters 
6.
Declaring a Function with Parameters
6.1.
JavaScript
6.2.
JavaScript
6.3.
JavaScript
7.
Function Parameters and Scope
7.1.
Multiple Parameters
7.2.
JavaScript
7.3.
Default Parameters
7.4.
JavaScript
7.5.
Function Rest Parameter
7.6.
JS
7.7.
Arguments Object
7.8.
JS
7.9.
Arguments Pass by Value
7.10.
JS
7.11.
Objects passed by Reference
7.12.
JS
8.
Frequently Asked Questions
8.1.
What is the difference between parameters and arguments in JavaScript?
8.2.
How does JavaScript handle extra arguments passed to a function?
8.3.
Can we call a function with more arguments than there are parameters?
8.4.
Do all parameters need to have a default value?
9.
Conclusion
Last Updated: Oct 28, 2024
Easy

JavaScript Function Parameter

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript functions are the building blocks of web development, enabling us to perform specific tasks, manipulate data, and create interactive experiences for users. But to fully harness their power, we need to understand function parameters and how they allow us to pass data into functions effectively. Whether you’re a beginner or an experienced developer, mastering function parameters is essential to writing clean, flexible, and reusable code. In this blog, we’ll explore the ins and outs of JavaScript function parameters.

What are Function Parameters in JavaScript?

Function parameters are inputs that we can pass into a function to influence its operation or outcome. When you declare a function, you can define it with parameters. These are essentially variables that the function will know about and can work with.

Syntax of Javascript Function Parameters

Here's the basic syntax for defining a function with parameters in JavaScript:

function functionName(parameter1, parameter2, ..., parameterN) {
    // Function body
}

Rules

  • Naming Conventions:
    • Parameters follow the same naming rules as variables (e.g., no spaces, no starting with a number).
    • Descriptive names make functions easier to understand.
  • Order of Parameters:
    • Parameters are defined in the order they should be passed to the function.
  • Optional Parameters:
    • You can define parameters without default values, but they’ll return undefined if not provided.
  • Default Values:
    • Default parameter values can be set by assigning a value in the parameter list, e.g., function example(param = 10).
  • Rest Parameters:
    • Using ... (spread operator) before a parameter name lets you collect all remaining arguments into an array.

Parameters 

  • functionName: The name of the function.
  • parameter1, parameter2, ..., parameterN: The parameters you want to define, separated by commas.

Declaring a Function with Parameters

Declaring a function with parameters in JavaScript follows a simple syntax. Here's an example:

  • JavaScript

JavaScript

function greet(name) {

  console.log("Hello, " + name);

}
You can also try this code with Online Javascript Compiler
Run Code

 

In this function, 'name' is the parameter. When we call this function and pass a name, it uses that name in its operation.

  • JavaScript
  • JavaScript

JavaScript

function greet(name) {

  console.log("Hello, " + name);

}
You can also try this code with Online Javascript Compiler
Run Code

JavaScript

greet("John");  // Output: Hello, John
You can also try this code with Online Javascript Compiler
Run Code

Function Parameters and Scope

The scope of function parameters is limited to the function they are declared in. In other words, you can't access a function parameter outside that function.

Multiple Parameters

Functions can have more than one parameter. For instance, consider a function to calculate the area of a rectangle:

  • JavaScript

JavaScript

function calculateArea(length, width) {

  let area = length * width;

  console.log("The area of the rectangle is " + area);

}



calculateArea(5, 4);  // Output: The area of the rectangle is 20
You can also try this code with Online Javascript Compiler
Run Code

 

In this example, the function calculateArea() has two parameters: length and width.

Default Parameters

JavaScript ES6 introduced default parameters. Default parameters allow you to assign a default value to the function parameter, which will be used if no argument is provided at function call:

  • JavaScript

JavaScript

function greet(name = "Stranger") {

  console.log("Hello, " + name);

}




greet();  // Output: Hello, Stranger
You can also try this code with Online Javascript Compiler
Run Code

 

Here, if we call the function without any arguments, it will use "Stranger" as a default name.

Function Rest Parameter

The rest parameter syntax allows a function to accept an indefinite number of arguments as an array, which makes it ideal for functions that need to handle variable-length argument lists.

Code Implementation:

  • JS

JS

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

console.log(sum(1, 2, 3));
console.log(sum(5, 10, 15, 20));
console.log(sum(10));
console.log(sum());
You can also try this code with Online Javascript Compiler
Run Code

        

Output:

6
50
10
0

Arguments Object

In JavaScript, the arguments object is a special object available within all non-arrow functions. It holds all the arguments passed to the function, even if the function doesn’t explicitly declare parameters.

Code Implementation:

  • JS

JS

function showArguments() {
console.log(arguments);
for (let i = 0; i < arguments.length; i++) {
console.log(`Argument ${i + 1}: ${arguments[i]}`);
}
}

showArguments(10, 20, 30);
You can also try this code with Online Javascript Compiler
Run Code

Output:

[Arguments] { '0': 10, '1': 20, '2': 30 }
Argument 1: 10
Argument 2: 20
Argument 3: 30

Arguments Pass by Value

In JavaScript, primitive data types (like numbers, strings, and booleans) are passed by value. This means a copy of the value is passed to the function, and any modification within the function does not affect the original variable.

Code Implementation:

  • JS

JS

function modifyValue(value) {
value += 10;
console.log("Inside function:", value); // Modified inside function
}

let num = 20;
modifyValue(num);
console.log("Outside function:", num); // Original value remains unchanged
You can also try this code with Online Javascript Compiler
Run Code

Output:

Inside function: 30
Outside function: 20

Objects passed by Reference

In JavaScript, objects (including arrays) are passed by reference. This means the function receives a reference to the original object, so changes to the object inside the function will affect the original object.

Code Implementation:

  • JS

JS

function modifyObject(obj) {
obj.name = "Alice";
console.log("Inside function:", obj); // Modified object
}

let person = { name: "Bob" };
modifyObject(person);
console.log("Outside function:", person); // Original object is changed
You can also try this code with Online Javascript Compiler
Run Code

Output:

Inside function: { name: 'Alice' }
Outside function: { name: 'Alice' }

Frequently Asked Questions

What is the difference between parameters and arguments in JavaScript?

Parameters are variables listed in a function's definition, while arguments are the actual values passed to the function when it’s called.

How does JavaScript handle extra arguments passed to a function?

JavaScript functions can receive extra arguments, which are accessible using the arguments object or rest parameters, allowing functions to handle a flexible number of arguments without errors.

Can we call a function with more arguments than there are parameters?

Yes, in JavaScript, you can call a function with more arguments than there are parameters. The extra arguments are ignored.

Do all parameters need to have a default value?

No, it's not necessary to provide default values for all parameters. You can choose to provide default values for some, all, or none of your parameters.

Conclusion

Understanding function parameters in JavaScript is crucial to creating dynamic and reusable code. They allow us to write flexible functions that can perform differently based on the inputs they receive. From declaring multiple parameters to setting up defaults, function parameters open up a world of possibilities in our code. By understanding how they work and how to use them, we can take our JavaScript coding skills to the next level. Remember, JavaScript is a tool, and learning to use it effectively is the key to unlocking its full potential.

Live masterclass