Let's say I have a function myfunc
that can take up to two parameters: arg1
and arg2
:
var myfunc = function (arg1,arg2) {
console.log(arg1,arg2);
};
I want to call the function using only arg2
. However, when I run this, the input is passed to the first argument, and myfunc
will log "input" undefined
rather than undefined "input"
as expected.
myfunc(arg2 = 'input')
> "input" undefined
Does Javascript support this type of input?
Javascript doesnot support the parameters with name. But you can achieve same thing using Unpacking fields from objects passed as function parameter and passing object to function
var myfunc = function ({arg1,arg2}) {
console.log(arg1,arg2);
};
myfunc({arg2:"input"})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With