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

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

To test if a parameter is provided by a function, use the if condition and check with “undefined”.

Example

Following is the code −

function checkingParameter(parameter) {
   if (parameter !== undefined) {
      console.log("Parameter is provided.");
   } else {
      console.log("Parameter is not provided.")
   }
}
checkingParameter();
checkingParameter("JavaScript");

To run the above program, you need to use the below command −

node fileName.js.

Here, my file name is demo327.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo327.js
Parameter is not provided.
Parameter is provided.
raja
Published on 27-Oct-2020 14:41:22
Advertisements