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

196
Visualizações
I can't understand why it returns undefined

I am trying to add 2 to given array. But the code returns undefined when run. I am trying to learn ES6, I couldn't find the problem. I need help.

const mapSomething = (arr) => {
  arr.map(n => {
    return n + 2;
    
  })
}

// It have to return [3, 4, 5] but it returns undefined
console.log(mapSomething([1, 2, 3]));
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It is logging undefined because the function is not returning any value.

You have to return the result from Array.map:

const mapSomething = (arr) => {
  return arr.map(n => {
    return n + 2;
  })
}

// It have to return [3, 4, 5] but it returns undefined
console.log(mapSomething([1, 2, 3]));

Also take note that the return statement in your arrow function is redundant, since there is only one expression being evaluated:

const mapSomething = (arr) => {
  return arr.map(n => n + 2)
}

// It have to return [3, 4, 5] but it returns undefined
console.log(mapSomething([1, 2, 3]));

about 4 years ago · Juan Pablo Isaza Relatório

0

You don't have a return statement in the function, so it returns undefined by default.

If an arrow function is a single expression that you want to return, you shouldn't put {} around it. That makes it a normal function body, which requires an explicit return statement (like you have with n + 2).

const mapSomething = (arr) => arr.map(n => n + 2);

// It have to return [3, 4, 5] but it returns undefined
console.log(mapSomething([1, 2, 3]));

about 4 years ago · Juan Pablo Isaza Relatório

0

const mapSomething = (arr) => {
    const arrData = arr.map(n => {
      return n + 2;     
    })

    return arrData
  }
  
  // It have to return [3, 4, 5] but it returns undefined
  console.log(mapSomething([1, 2, 3]))
  
  //NOTE: you where not returning anything from the function itself

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