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. It implies that the function's parameters will have undefined values by default, if you don't supply any arguments to it.

Using strict inequality (! ==)in JavaScript

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. Contrary to the inequality operator, the strict inequality operator consistently treats operands of various types as distinct.

Syntax

Following is the syntax for strict inequality(!==)

x! == y

Example

In the following example, we are running the script using strict inequality (! ==) to check parameter is provided to a function or not.

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

When the script gets executed, it will generate an output consisting of two values printed on the web-browser, triggered by an event that gets triggered on executing the script that checks, if a parameter is provided or not and returns a value.

Example

Consider the following example, where we are using the typeof operator (checks the data type of a variable, object or literal value) to check that the parameter is not equal to the string "undefined”, for instance if (typeof param!== "undefined"). If it matches the condition, the parameter gets passed to the function.

<!DOCTYPE html>
<html>
<body>
   <script>
      function checkPara(para) {
         if (typeof para !== 'undefined') {
            return para;
         }
      }
      document.write(typeof undefined +"<br>");
      document.write(typeof (() => {})+"<br>");
      document.write(typeof null +"<br>");
      document.write(typeof [] +"<br>");
      document.write(typeof '' +"<br>");
      document.write(typeof 0 +"<br>");
   </script>
</body>
</html>

On running the above script, the web-browser displays the type of parameter mentioned in the script, and then pass them to the function activated by the event that gets triggered on running the script.

Example

Let’s look at the following example, where we are checking if a parameter is used or not. We are invoking the function, passing undefined, and treating them as the value of the parameter.

<!DOCTYPE html>
<html>
<body>
   <script>
      function paratest(...test) {
         if (test.length) {
            return `exists`;
         } else {
            return `not exists`;
         }
      }
      document.write(paratest(123)+"<br>");
      document.write(paratest(undefined)+"<br>");
      document.write(paratest()+"<br>");
   </script>
</body>
</html>

When the script gets executed, the event gets triggered, checks the condition, and displays the value that the parameter exists or not.

Updated on: 18-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements