If you run the below code in browser's console:
function named_param(a, b=null, c=5) {
console.log("a="+a);
console.log("b="+b);
console.log("c="+c);
}
named_param(3, c=10)
The output received is:
a=3
b=10
c=5
The output I am looking for, is:
a=3
b=null
c=10
I saw the below two URLs and tried a few workaround but it didn't work
Javascript Function Parameters Not Working
Named parameters in javascript
Let me know the steps I am doing wrong here. Tried the below codes but didn't work: A:
function named_param(a, {b=null, c=5}) {
console.log("a="+a);
console.log("b="+b);
console.log("c="+c);
}
named_param(3, c=10)
B:
function named_param(a, {b=null, c=5}={}) {
console.log("a="+a);
console.log("b="+b);
console.log("c="+c);
}
named_param(3, c=10)
When using the destructuring solution, you must pass an actual object. named_param(3, c=10)
simply is invalid syntax (well, it's valid, but it's an assignment to a global c
variable that is passed as the second argument - no naming).
function named_param(a, {b=null, c=5}={}) {
console.log("a="+a);
console.log("b="+b);
console.log("c="+c);
}
named_param(3)
named_param(3, {b:"hi"})
named_param(3, {c:10})
named_param(3, {b:"hi", c:10})