-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
In JavaScript, function parameters are variables listed in a function definition that allow the function to accept input values. They make functions dynamic and reusable by letting them operate on different values each time they are called. Understanding function parameters is essential for writing flexible, maintainable, and modular code.
This tutorial explains function parameters in detail, including their syntax, types, default values, practical examples, common mistakes, best practices, and real-world applications.
Function parameters are important because they:
Allow functions to accept input values and behave dynamically.
Reduce repetitive code by reusing the same function with different arguments.
Enable modular and maintainable programs.
Facilitate calculations, data processing, and other logical operations.
Form the basis for advanced concepts like rest parameters, destructuring, and callback functions.
Without parameters, functions would be limited to static operations, reducing code flexibility.
Parameters are defined inside the parentheses of a function definition:
function greet(name) {
console.log("Hello, " + name + "!");
}
Here, name is a parameter. When the function is called with an argument, it replaces the parameter:
greet("Aarushi");
greet("Priya");
Output:
Hello, Aarushi!
Hello, Priya!
Functions can accept multiple parameters separated by commas:
function add(a, b) {
return a + b;
}
console.log(add(10, 15));
console.log(add(25, 35));
Output:
25
60
The values passed when calling a function are called arguments, while the placeholders in the function definition are parameters.
You can assign default values to parameters. If an argument is not provided, the default value is used:
function greet(name = "Guest") {
console.log("Hello, " + name + "!");
}
greet();
greet("Ananya");
Output:
Hello, Guest!
Hello, Ananya!
Default parameters help handle cases where some arguments may not be provided.
When the number of arguments is unknown, rest parameters allow you to capture them into an array:
function sum(...numbers) {
let total = 0;
for (let num of numbers) {
total += num;
}
return total;
}
console.log(sum(10, 20, 30));
console.log(sum(5, 15, 25, 35));
Output:
60
80
The ...numbers syntax collects all remaining arguments into an array for processing.
JavaScript allows destructuring objects or arrays directly in function parameters.
function displayStudent({ name, age }) {
console.log(`${name} is ${age} years old.`);
}
const student = { name: "Isha", age: 15 };
displayStudent(student);
Output:
Isha is 15 years old.
function displayColors([primary, secondary]) {
console.log(`Primary: ${primary}, Secondary: ${secondary}`);
}
const colors = ["Red", "Blue"];
displayColors(colors);
Output:
Primary: Red, Secondary: Blue
Destructuring makes the function code cleaner and more readable.
Every non-arrow function has an arguments object that contains all arguments passed to the function:
function multiply() {
let product = 1;
for (let i = 0; i < arguments.length; i++) {
product *= arguments[i];
}
return product;
}
console.log(multiply(2, 3, 4));
Output:
24
While arguments is useful, modern JavaScript prefers rest parameters for better readability and flexibility.
Confusing parameters with arguments. Parameters are defined in the function; arguments are passed during calls.
Forgetting default values, leading to undefined when no argument is provided.
Using arrow functions expecting an arguments object (arrow functions do not have one).
Overwriting parameters inside the function unnecessarily.
Passing too many or too few arguments without handling them correctly.
Use descriptive parameter names for clarity.
Prefer default values to avoid undefined values.
Use rest parameters when the number of arguments is variable.
Avoid using the arguments object in modern JavaScript.
Use destructuring to extract values from objects or arrays directly.
Keep the number of parameters manageable for readability.
Accepting user input in functions for greetings, calculations, or validation.
Summing multiple numbers or performing operations on a variable number of arguments.
Extracting values from objects or arrays passed to functions.
Creating modular code where the same function can handle different inputs.
Processing API data or events with variable arguments.
Function parameters are a core concept in JavaScript that allow functions to accept input dynamically, making them reusable and flexible. Parameters can be single or multiple, can have default values, and can use rest parameters for variable-length arguments. JavaScript also supports destructuring in parameters for objects and arrays, making function definitions cleaner. Proper use of function parameters reduces repetition, improves modularity, and makes code easier to maintain and understand. Mastering function parameters is essential for building scalable and efficient JavaScript applications.
Q1. What is the difference between a function parameter and an argument in JavaScript?
Q2. How do you set a default parameter value in a function?
Q3. Write a function that multiplies two numbers with default values of 1 for both.
Q4. What happens when a function is called with fewer arguments than parameters?
Q5. How can you use rest parameters to handle an unknown number of arguments?
Q6. Write a function that returns the sum of all arguments passed using the rest operator.
Q7. How do destructured parameters work when accepting objects as input?
Q8. Create a function that accepts an object and logs its properties using destructuring.
Q9. Can you combine normal parameters with rest parameters? Show an example.
Q10. What is the result of calling a function with more arguments than defined parameters?
JS Basics
JS Variables & Operators
JS Data Types & Conversion
JS Numbers & Math
JS Strings
JS Dates
JS Arrays
JS Control Flow
JS Loops & Iteration
JS Functions
JS Functions
Function Definitions
Function Parameters
Function Invocation
Function Call
Function Apply
Function Bind
Function Closures
JS Arrow Function
JS Objects
JS Objects
JS Object Properties
JS Object Methods
JS Object Display
JS Object Constructors
Object Definitions
Object Get / Set
Object Prototypes
Object Protection
JS Classes & Modules
JS Async Programming
JS Advanced
JS Destructuring
JS Bitwise
JS RegExp
JS Precedence
JS Errors
JS Scope
JS Hoisting
JS Strict Mode
JS this Keyword
JS HTML DOM
DOM Intro
DOM Methods
DOM Document
DOM Elements
DOM HTML
DOM Forms
DOM CSS
DOM Animations
DOM Events
DOM Event Listener
DOM Navigation
DOM Nodes
DOM Collections
DOM Node Lists
JS BOM (Browser Object Model)
JS Web APIs
Web API Intro
Web Validation API
Web History API
Web Storage API
Web Worker API
Web Fetch API
Web Geolocation API
JS AJAX
AJAX Intro
AJAX XMLHttp
AJAX Request
AJAX Response
AJAX XML File
AJAX PHP
AJAX ASP
AJAX Database
AJAX Applications
AJAX Examples
JS JSON
JSON Intro
JSON Syntax
JSON vs XML
JSON Data Types
JSON Parse
JSON Stringify
JSON Objects
JSON Arrays
JSON Server
JSON PHP
JSON HTML
JSON JSONP
JS Graphics & Charts
