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

149
Visualizações
How to add if statement in arrow function

i'm a beginner in JS and was wondering what is the syntax of an arrow function with an if statement with a regular function such as function

getStringLength(string){
  let stringLength;
  if (string.length === 1){
     stringLength = `La chaîne contient qu'un seul caractère`;
  } else {
     stringLength = `La chaîne contient ${string.length} caractères`;
  }
     return stringLength;
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

That would be

const getStringLength = (string) => {
  let stringLength;
    if (string.length === 1){
            stringLength = `La chaîne contient qu'un seul caractère`;
    } else {
            stringLength = `La chaîne contient ${string.length} caractères`;
    }
    return stringLength;
}
about 4 years ago · Juan Pablo Isaza Relatório

0

With ternary this would look like this using an arrow function.

Note that with arrow functions you can avoid using the return keyword

  
const getStringLength = (string) => string.length === 1 ? `La chaîne contient qu'un seul caractère` : `La chaîne contient ${string.length} caractères`

console.log(getStringLength('a'))
console.log(getStringLength('abcdef'))

about 4 years ago · Juan Pablo Isaza Relatório

0

You can also do it in one line

const getStringLength = (string) =>
  string.length === 1 ?
  `La chaîne contient qu'un seul caractère` :
  `La chaîne contient ${string.length} caractères`
  
  
console.log(getStringLength('a'))
console.log(getStringLength('ab'))
  

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