Skip to main content

Archive

Show more

How to set default argument values in JavaScript functions

how-to-set-default-argument-values-in-javascript-functions


Question: How to set default argument values in JavaScript functions

Answer:

function to set default value.

function defaultValuefunction(a, b) {
 if (a === undefined) 
   a = "a default value";
 
 if (b === undefined) 
   b = "b default value";
  
 console.log(a);
 console.log(b);
}

function call with parameter.

defaultValuefunction(12, 13); //function call with parameter

Output:

12
13

function call without parameter.

defaultValuefunction(); //function call without parameter

Output:

a default value
b default value


We try to provide you the best content, if there is any mistake in this article or there is any mistake in code, then let us know.

Comments