Computer >> Computer tutorials >  >> Programming >> Javascript

Setting a default variable value to undefined in a function - JavaScript?


Let’s say we are passing the following values to a function −

300
undefined

Example

We will now set the default value. Following is the code −

function showValue(value)
{
   if (value===undefined) {
      value=100000;
   }
   else {
      value=value;
   }
   return value;
} 
console.log("The value="+showValue(300));
console.log("The value="+showValue(undefined));

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

node fileName.js.

Here, my file name is demo246.js.

Output

This will produce the following output on console −

PS C:\Users\Amit\javascript-code> node demo246.js
The value=300
The value=100000