soy un principiante en JS y me preguntaba cuál es la sintaxis de una función de flecha con una declaración if con una función normal como función
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; }Eso sería
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; }Con ternario, esto se vería así usando una función de flecha.
Tenga en cuenta que con las funciones de flecha puede evitar el uso de la palabra clave de return
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'))También puedes hacerlo en una sola línea.
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'))