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

253
Visualizações
Difference between these two functions in JS that I'm querying from firestore

So I was making a get user email function and came up with this.

This is the function that works perfectly.

const getMail = (users, userLoggedIn) =>
    users?.filter((userToFilter) => userToFilter !== userLoggedIn?.email)[0];

export default getMail;

Here is the 2nd function, that is not working as it's returning undefined.

const getMail = (users, userLoggedIn) => {
    users?.filter((userToFilter) => userToFilter !== userLoggedIn?.email)[0];
}

export default getMail;
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The second function is not returning anything, you will have to use return statement when wrapping the function body in curly brackets.

If you do not put brackets around your arrow function body the return statement can be ommitted. So the first function works.

In the second function you wrote curly brackets { and you would need to add a return statement.

So these functions are equal:

// #1
const getMail = (users, userLoggedIn) =>
  // curly brackets ommitted. THe function only consists of "1" statement 
  // and the result will automatically returned.
  users?.filter((userToFilter) => userToFilter !== userLoggedIn?.email)[0];

export default getMail;

// #2 
const getMail = (users, userLoggedIn) => {
    // you need to explicitly use "return" to return the value.
    return users?.filter((userToFilter) => userToFilter !== userLoggedIn?.email)[0];
}

export default getMail;
about 4 years ago · Juan Pablo Isaza Relatório

0

You're using a shorthand without even knowing it. Without the {} after the arrow it will by default return whatever comes after the arrow. Otherwise you have to use the return statement.

const getMail = (users, userLoggedIn) => {
    return users?.filter((userToFilter) => userToFilter !== userLoggedIn?.email)[0];
}

export default getMail;
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