Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

296
Views
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.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The syntax is slightly different:

> function test({a=1, b=2, c=3}) { console.log(a+b+c); }
undefined
> test({b:"b"})
1b3
undefined
about 4 years ago · Juan Pablo Isaza Report

0

Parameters are positional in javascript - the first parameter will always be 'a', the second 'b' and so on.

One way to achieve named parameters would be to change the signature of the function test to accept an object:

function test(obj) {
    const { a='1', b='2', c='3' } = obj
    console.log(a + b + c)
}

Calling this with

test({ b: 'b' })

will yield '1b3'

about 4 years ago · Juan Pablo Isaza Report

0

Keyword arguments are not supported in JS, arguments are resolved by their position in JS.

You could instead pass in an object and destructure the properties from it and also assign default values.

function runThis() {
  test({ b: "b" });
}

function test(inputObj) {
  const { a = "1", b = "2", c = "3" } = inputObj;
  console.log(a + b + c);
}

runThis();

You could also do the destructuring more succinctly, as shown below:

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

Also, console.println is not a thing in JS, you can use console.log instead.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!