Passing empty parameter to a method in JavaScript

In JavaScript, you can pass empty parameters to a method by simply omitting them when calling the function. This is useful when working with functions that have default parameters or when you want to rely on the function's default behavior.

Understanding Default Parameters

Default parameters allow functions to have fallback values when no arguments are provided or when undefined is passed.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Passing Empty Parameters</title>
    <style>
        body {
            font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
            padding: 20px;
        }
        .result {
            font-size: 18px;
            font-weight: 500;
            color: blueviolet;
            margin: 10px 0;
        }
        button {
            padding: 10px 20px;
            margin: 5px;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1>Passing Empty Parameters to a Method</h1>
    
    <div class="result" id="result1"></div>
    <button onclick="testEmptyParams()">Call with Empty Parameters</button>
    
    <div class="result" id="result2"></div>
    <button onclick="testWithValues()">Call with Values</button>
    
    <div class="result" id="result3"></div>
    <button onclick="testMixedParams()">Call with Mixed Parameters</button>

    <script>
        function multiply(a = 2, b = 4) {
            return a * b;
        }
        
        function greet(name = "Guest", greeting = "Hello") {
            return greeting + ", " + name + "!";
        }
        
        function testEmptyParams() {
            let result = multiply(); // No parameters passed
            document.getElementById("result1").innerHTML = 
                "multiply() with no parameters: " + result;
        }
        
        function testWithValues() {
            let result = multiply(5, 3); // Both parameters passed
            document.getElementById("result2").innerHTML = 
                "multiply(5, 3) with parameters: " + result;
        }
        
        function testMixedParams() {
            let result1 = multiply(7); // Only first parameter
            let result2 = greet(); // No parameters
            document.getElementById("result3").innerHTML = 
                "multiply(7): " + result1 + "<br>" + 
                "greet(): " + result2;
        }
    </script>
</body>
</html>

Different Ways to Pass Empty Parameters

You can handle empty parameters in several ways:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Empty Parameter Methods</title>
    <style>
        body { font-family: Arial, sans-serif; padding: 20px; }
        .output { margin: 10px 0; padding: 10px; background-color: #f0f0f0; }
    </style>
</head>
<body>
    <h1>Methods of Handling Empty Parameters</h1>
    <button onclick="demonstrateAll()">Show All Methods</button>
    <div id="output"></div>

    <script>
        function calculateArea(length = 5, width = 3) {
            return length * width;
        }
        
        function demonstrateAll() {
            let output = document.getElementById("output");
            let results = "";
            
            // Method 1: No parameters
            results += "<div class='output'>No parameters: " + calculateArea() + "</div>";
            
            // Method 2: Passing undefined explicitly
            results += "<div class='output'>Undefined parameters: " + calculateArea(undefined, undefined) + "</div>";
            
            // Method 3: Partial parameters
            results += "<div class='output'>First parameter only: " + calculateArea(8) + "</div>";
            
            // Method 4: Skip first parameter with undefined
            results += "<div class='output'>Skip first parameter: " + calculateArea(undefined, 6) + "</div>";
            
            output.innerHTML = results;
        }
    </script>
</body>
</html>

Comparison of Parameter Passing Methods

Method Syntax Result Use Case
No parameters func() Uses all defaults Most common
Undefined explicitly func(undefined) Uses default for that param Skip specific parameters
Partial parameters func(value) Uses provided + defaults Override some defaults
Null parameter func(null) Uses null (not default) Explicitly pass null

Key Points

  • Empty parameters automatically use default values if defined
  • Passing undefined explicitly triggers default parameters
  • Passing null does NOT trigger default parameters
  • You can skip parameters by not providing them or passing undefined

Conclusion

Passing empty parameters to JavaScript methods is straightforward with default parameters. Simply omit arguments when calling the function, and the default values will be used automatically. This feature makes functions more flexible and easier to use.

Updated on: 2026-03-15T23:18:59+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements