How to pass specified parameters in Javascript

I would like to pass value to specified parameters and use default value by optional parameters for the rest.

A sample is made as below.

Current result is ‘b23’.

But I would like to obtain the result of ‘1b3’.

function runThis() {
    test(b='b');
}
function test(a='1',b='2',c='3'){
    console.println(a+b+c);
}

I also try to run test({b:'b'}) and test({b:='b'}), resulting SyntaxError.

Thank you for your help.

>Solution :

The syntax is slightly different:

> function test({a=1, b=2, c=3}) { console.log(a+b+c); }
undefined
> test({b:"b"})
1b3
undefined

Leave a Reply