Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

308
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda