How to test if a parameter is provided to a function in JavaScript?

Testing if a parameter is provided to a function in JavaScript is an important part of writing effective code. It allows you to ensure that the arguments passed into your functions are valid and properly formatted for use within the function. In this article, we will discuss different methods for testing whether or not a parameter has been provided to a function in JavaScript.

Let us understand the term "Parameter". A parameter is a named entity in a function definition that specifies an argument which the function can accept. It acts as a variable within the function and its value is passed to the function when it is called.

A parameter's default value in JavaScript is undefined. This means that if you don't supply any arguments to a function, its parameters will have undefined values by default.

Using Strict Inequality (!==)

Strict inequality (!==) in JavaScript is a comparison operator that returns true only if the values of two operands are not equal and of the same type. Unlike the inequality operator, the strict inequality operator consistently treats operands of different types as distinct.

Syntax

x !== y

Example

In the following example, we use strict inequality (!==) to check if a parameter is provided to a function.

<!DOCTYPE html>
<html>
<body>
   <script>
      function checkPara(para) {
         if (para !== undefined) {
            return para;
         } else {
            return "No parameter provided";
         }
      }
      document.write(checkPara(23) + "<br>"); 
      document.write(checkPara());
   </script>
</body>
</html>
23
No parameter provided

Using typeof Operator

The typeof operator checks the data type of a variable, object, or literal value. We can use it to verify that a parameter is not equal to the string "undefined".

Example

<!DOCTYPE html>
<html>
<body>
   <script>
      function checkPara(para) {
         if (typeof para !== 'undefined') {
            return "Parameter provided: " + para;
         } else {
            return "No parameter provided";
         }
      }
      
      document.write(checkPara(42) + "<br>");
      document.write(checkPara() + "<br>");
      document.write("typeof undefined: " + typeof undefined + "<br>");
      document.write("typeof null: " + typeof null + "<br>");
      document.write("typeof 0: " + typeof 0);
   </script>
</body>
</html>
Parameter provided: 42
No parameter provided
typeof undefined: undefined
typeof null: object
typeof 0: number

Using Rest Parameters (...)

Rest parameters allow us to represent an indefinite number of arguments as an array. We can check the length of this array to determine if parameters were passed.

Example

<!DOCTYPE html>
<html>
<body>
   <script>
      function paratest(...test) {
         if (test.length > 0) {
            return "Parameters exist: " + test.join(", ");
         } else {
            return "No parameters provided";
         }
      }
      
      document.write(paratest(123) + "<br>");
      document.write(paratest(undefined) + "<br>");
      document.write(paratest() + "<br>");
      document.write(paratest(1, 2, 3));
   </script>
</body>
</html>
Parameters exist: 123
Parameters exist: undefined
No parameters provided
Parameters exist: 1, 2, 3

Comparison of Methods

Method Handles null Handles 0/false Multiple Parameters
!== undefined Yes Yes No
typeof !== 'undefined' Yes Yes No
Rest parameters Yes Yes Yes

Conclusion

The most reliable method for checking if a parameter is provided is using strict inequality (!== undefined). For multiple parameters, rest parameters with length checking provides the most flexibility.

Updated on: 2026-03-15T23:19:00+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements