Tumu Dinakar Tumu Dinakar - 27 days ago 19
Javascript Question

how to create JQuery custom functions with Parameters

Hi I have question that how to create custom functions in jQuery

I have a difficult situation here,

var value1= [];
var value2= [];
var value3= [];


function functionName(first,second,three,four = []) {
}

$(".btn").click(function(e) {
// 1 st way
functionName(value1,value2,value3);
// 2nd way
functionName(value1,value2,value3,value4);

});


Can i use like this ?
I mean my function 'functionName' has 4 parameters but some cases it has only 3 parameter so in that case I want last parameter should consider as empty.

so I can use like this in JQuery ?

I am using Codeception Test cases but if I use four = [] its getting fail.

Answer

Yes you can do this . It is much to do with javascript rather than jQuery

check this snippet

function multiply(a, b = 1) {
  return a*b;
}

console.log(multiply(5));
console.log(multiply(5,8));

Hope this helps