Default Parameters in JavaScript function

Here is simple code snippet for JavaScript function with default parameter. Demo code given for use two parameter function with default value. You can use both parameter default value or first or second default parameter value.

Function with default parameter

function test(para1, para2) {
  para1 = typeof para1 !== 'undefined' && para1 != '' ?  para1 : 'para1_default';
  para2 = typeof para2 !== 'undefined' && para2 != '' ?  para2 : 'para2_default';

  alert('para1 = '+para1+', para2 = '+para2);
}

Use both default parameter

Test both default parameter: test();

Use first default and second passing parameter

Test first default and second passing parameter: test( ”, ‘mypara1’ );