How to determine if an argument is sent to the JavaScript function?

To determine if an argument is sent to a JavaScript function, you can use several approaches including default parameters, checking for undefined, or using the arguments object.

Using Default Parameters

Default parameters provide fallback values when arguments are not passed or are undefined:

<!DOCTYPE html>
<html>
<body>
    <script>
        function display(arg1 = 'Tutorials', arg2 = 'Learning') {
            document.write(arg1 + ' ' + arg2 + "<br>");
        }
        
        display('JavaScript', 'Programming'); // Both arguments provided
        display('JavaScript');                 // Only first argument
        display();                            // No arguments
    </script>
</body>
</html>
JavaScript Programming
JavaScript Learning
Tutorials Learning

Checking for undefined

You can explicitly check if parameters are undefined to determine if they were passed:

<!DOCTYPE html>
<html>
<body>
    <script>
        function checkArguments(arg1, arg2) {
            if (arg1 === undefined) {
                document.write("arg1 was not provided<br>");
            } else {
                document.write("arg1: " + arg1 + "<br>");
            }
            
            if (arg2 === undefined) {
                document.write("arg2 was not provided<br>");
            } else {
                document.write("arg2: " + arg2 + "<br>");
            }
            document.write("<br>");
        }
        
        checkArguments("Hello", "World");
        checkArguments("Hello");
        checkArguments();
    </script>
</body>
</html>
arg1: Hello
arg2: World

arg1: Hello
arg2 was not provided

arg1 was not provided
arg2 was not provided

Using arguments.length

The arguments.length property tells you exactly how many arguments were passed:

<!DOCTYPE html>
<html>
<body>
    <script>
        function countArguments() {
            document.write("Arguments passed: " + arguments.length + "<br>");
            for (let i = 0; i < arguments.length; i++) {
                document.write("Argument " + i + ": " + arguments[i] + "<br>");
            }
            document.write("<br>");
        }
        
        countArguments("A", "B", "C");
        countArguments("X");
        countArguments();
    </script>
</body>
</html>
Arguments passed: 3
Argument 0: A
Argument 1: B
Argument 2: C

Arguments passed: 1
Argument 0: X

Arguments passed: 0

Comparison of Methods

Method Use Case Modern Approach
Default Parameters Provide fallback values Yes (ES6+)
undefined Check Explicit argument validation Yes
arguments.length Count total arguments Legacy (use rest parameters instead)

Conclusion

Default parameters are the most modern and clean approach for handling missing arguments. Use undefined checks when you need explicit control over argument validation.

Updated on: 2026-03-15T21:49:24+05:30

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements