Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda