Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to pass the value 'undefined' to a function with multiple parameters in JavaScript?
In this tutorial, we will learn how to pass the value 'undefined' to a function with multiple parameters in JavaScript.
In JavaScript, the data type of 'undefined' is primitive. When a variable is declared, JavaScript automatically assigns the value 'undefined' to it. If a function has multiple parameters and one of the parameters' values is not available at the moment, then we need to omit that parameter's value from the function call. But if we omit the parameter's value with a blank space, the JavaScript will show an error.
The Problem
function abc(param1, param2, param3) {
console.log(param1, param2, param3);
}
// Will not work this way
abc(10, ,30) // Throws an error
In the above syntax, "abc" is a function that has three parameters. We called this function with a missing parameter, which causes a syntax error.
In this tutorial, we will discuss two different approaches to overcome this problem and how 'undefined' is passed to a function with multiple parameters:
Passing an 'undefined' value directly into the arguments.
Passing a variable that has a value of 'undefined'.
Method 1: Passing 'undefined' Value Directly
If the function has multiple parameters and users don't want to use all of them or one of the parameters is missing, then passing an 'undefined' value directly in the arguments is a good way to deal with this issue. We can pass undefined values in the arguments like any other data type, such as a number, string, or Boolean.
Syntax
function abc(param1, param2, param3) {
console.log(param1, param2, param3);
}
// Passing undefined values directly in the arguments
abc(10, undefined, 30)
Example
In the below given example, we have passed the value 'undefined' directly to a function with multiple parameters in JavaScript.
<html>
<body>
<p>function showArguments(param1, param2, param3)</p>
<button onclick="showArguments('Tutorialspoint', undefined, 10)"> Show Arguments</button>
<p id="root"></p>
<script>
function showArguments(param1, param2, param3) {
let str = 'Argument 1: ' + param1 + '<br />'
str += ' Argument 2: ' + param2 + '<br />'
str += ' Argument 3: ' + param3 + '<br />'
document.getElementById('root').innerHTML = str
}
</script>
</body>
</html>
Argument 1: Tutorialspoint Argument 2: undefined Argument 3: 10
Method 2: Using a Variable with Undefined Value
We can pass the value 'undefined' to a function with multiple parameters using a variable that has the value of 'undefined'. In JavaScript, whenever a user declares a variable without assigning a value, it automatically contains the value of 'undefined'.
Syntax
function abc(param1, param2, param3) {
console.log(param1, param2, param3);
}
let undef; // stores the value 'undefined'
// Passing 'undefined' values using a variable
abc(10, undef, 30)
In the above syntax, "abc" is a function with three parameters. "undef" is a variable containing the value of 'undefined'. We passed the "undef" variable in the arguments of the "abc" function.
Example
In the below given example, we have passed the value 'undefined' to a function with multiple parameters using a variable containing the value of 'undefined'.
<html>
<body>
<p>function showArguments(param1, param2, param3)</p>
<button onclick="showArguments('Tutorialspoint', undef, 10)"> Show Arguments</button>
<p id="root"></p>
<script>
// 'undefined' variable
let undef;
function showArguments(param1, param2, param3) {
let str = ' Argument 1: ' + param1 + '<br />'
str += ' Argument 2: ' + param2 + '<br />'
str += ' Argument 3: ' + param3 + '<br />'
document.getElementById('root').innerHTML = str
}
</script>
</body>
</html>
Argument 1: Tutorialspoint Argument 2: undefined Argument 3: 10
Key Points
Variables that we declare during function declaration are called parameters. The real values we pass in a function call are called arguments.
You cannot leave blank spaces between commas when calling a function - this causes a syntax error.
Both methods achieve the same result - passing undefined to skip a parameter.
Conclusion
Both approaches allow you to pass 'undefined' to functions with multiple parameters. Use the direct approach for clarity, or the variable approach when you need to reuse the undefined value multiple times.
